2017-04-16 89 views
1

我试图从我的'data.json'文件以json格式打印数据。用我的PHP文件(alldata.php),我可以获得所有数据(数组)的漂亮打印。但是,我希望你能帮助我的是如何获取特定的数组名称及其对象/内容。如何通过解析URL中的数组名称从JSON文件中获取数据

我alldata.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" 

}] 
} 

在PHP中,我如何通过使用URL,如“alldata.php搜索=球员?” 得到一个阵列上,比如“运动员”与内容下面是一个代码示例....

//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/>'; } 
} 

?> 

一旦访问这些数据,你可以很容易地做你想要做什么用......