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

?> 

あなたがデータにアクセスしたら、あなたは簡単にあなたがそれでやりたいことができます...

関連する問題