2017-04-16 1 views
1

'data.json'파일에서 json 형식으로 데이터를 인쇄하려고합니다. 내 PHP 파일 (alldata.php), 나는 모든 데이터 (배열) 꽤 인쇄 얻을 수 있습니다. 하지만 특정 배열 이름을 가져 오는 방법과 객체/내용을 얻는 방법을 알려주고 싶습니다.URL에서 배열 이름을 파싱하여 JSON 파일에서 데이터를 가져 오는 방법

내 alldata.php은 다음과 같습니다 : 나는 그런 '? alldata.php 검색 = 선수 으로 URL을 사용하여 콘텐츠를 예를 들어 "선수"배열을 얻을 수있는 방법

PHP에서
{ 

"players": [ 
    { 
     "name": "Marcos Alonso", 
     "position": "Left-Back", 
     "nationality": "Spain", 
     "marketValue": "9,000,000 €", 
     "created": "2017-04-15 10:04:58" 
    }], 

"articles": [ 
{ 
    "author": "Stephen Walter", 
    "title": "Disruptive stag party revellers thrown off plane at Manchester Airport", 
    "url": "http://www.telegraph.co.uk/news/2017/04/15/disruptive-stag-party-revellers-thrown-plane-manchester-airport/", 
    "publishedAt": "2017-04-15T09:25:10Z" 
}], 

land": [ 
{ 
    "state": "Somewhr", 
    "found": "1889", 
    "area": "6,812", 
    "empl": "1,325", 
    "ppl": "16,842" 

}] 
} 

, 여기에 코드 샘플 ....

//get content of the JSON API using file_get_contents() 
$url = ('myJson.json'); 
$jsondata = file_get_contents($url); 
//convert json object to php associative array 
$data = json_decode($jsondata, true); 

what do I do here to query the data.json file for a specific array????? 

header('Content-Type: application/json; charset=UTF-8'); 
$json_string = json_encode($????????????, JSON_PRETTY_PRINT); 
print $json_string; 

덕분이다

+0

이 배열은 항상 여러 데이터 (같은 나무)와 같은를 찾고? – OldPadawan

답변

0

내가 제대로 이해하면 당신은 무엇을 의미하는지, 그리고 배열은 항상 같은 나무, 당신은 데이터에 액세스이 wilp 도움이있는 경우 :

<?php 

error_reporting(E_ALL); 
ini_set("display_errors", 1); 

$array = array(

0 => array(
    "players" => array(
    "name" => "Marcos Alonso", 
    "position" => "Left-Back", 
    "nationality" => "Spain", 
    "marketValue" => "9,000,000 €", 
    "created" => "2017-04-15 10:04:58" 
    ), 
    "articles" => array(
    "author" => "Stephen Walter", 
    "title" => "Disruptive stag party revellers thrown off plane at Manchester Airport", 
    "url" => "http://www.telegraph.co.uk/news/2017/04/15/", 
    "publishedAt" => "2017-04-15T09:25:10Z" 
    ), 
    "land" => array(
    "state" => "Somewhr", 
    "found" => "1889", 
    "area" => "6,812", 
    "empl" => "1,325", 
    "ppl" => "16,842" 
    ) 
), 

1 => array(
    "players" => array(
    "name" => "Sebastian Vettel", 
    "position" => "Driver", 
    "nationality" => "Germany", 
    "marketValue" => "9,000,000 €", 
    "created" => "2013-03-15 11:04:52" 
    ), 
    "articles" => array(
    "author" => "Stephen Walter", 
    "title" => "Disruptive stag party revellers thrown off plane at Manchester Airport", 
    "url" => "http://www.telegraph.co.uk/news/2017/04/15/", 
    "publishedAt" => "2017-04-15T09:25:10Z" 
    ), 
    "land" => array(
    "state" => "Somewhr", 
    "found" => "1889", 
    "area" => "6,812", 
    "empl" => "1,325", 
    "ppl" => "16,842" 
    ) 
) 

); 
/* end of array */ 

$data1 = json_encode($array); /* just checking - not needed after that */ 
$data = json_decode($data1, true); /* just checking - not needed after that */ 

$needle = "articles"; /* should be $needle = $_GET['search']; and checked before use */ 

//print_r($data); /* just checking */ 

foreach($data as $value){ /* we access 1st level */ 
echo '** Needle is: '.$needle.' **<br/>'; 
    foreach($value[$needle] as $sub_key => $sub_data){ /* we access 2nd level */ 
echo $sub_key.'-->'.$sub_data.'<br/>'; } 
} 

?> 

사용자가 데이터에 액세스하면, 당신은 쉽게 당신이 그것으로 원하는 것을 할 수 있습니다 ...

관련 문제