2017-11-05 26 views
0

私は結果を得るためにWikipedia Webサービスを使用しています。 PHPで取得したJSONファイルにデータを追加するのが好きです。データを追加する前にJSONファイルをデコードする必要がありますが、データを次の形式で追加する方法は示されていません。URL、PHPから取得したJSONにデータを追加

私のPHPコード:?

<?php 

//---------------------Wikipedia Web Service------------------- 
$json_array = array(); 
$answer_data = array(); 

$command_text = ""; 

if(isset($_POST["command"]) && !empty($_POST["command"])){ 
    $command_text = $_POST["command"]; 
} 

//JSON link and get data and then send data back to client 
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=' . $command_text .'&format=json'; 
$json = file_get_contents($url); 
$json_decode = json_decode($json, true); 



$newer['query']['search'][20] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!"); 

array_push($json_decode, $newer); 

//----------------OUTPUT JSON----------------- 
//header function converts JSON to objects (no need JSON.parse() javascript function)! 
header("Content-type:application/json"); 
echo json_encode($json_decode); 

>

JSONファイル:

{"batchcomplete":"","continue":{"sroffset":10,"continue":"-||"},"query":{"searchinfo":{"totalhits":20894},"search":[{"ns":0,"title":"Owl","pageid":37654,"size":52826,"wordcount":6200,"snippet":"<span class=\"searchmatch\">Owls</span> are birds from the order Strigiformes, which includes about 200 species of mostly solitary and nocturnal birds of prey typified by an upright stance","timestamp":"2017-11-02T10:44:00Z"},{"ns":0,"title":"Owl (disambiguation)","pageid":4577780,"size":6478,"wordcount":843,"snippet":"<span class=\"searchmatch\">Owls</span> are nocturnal birds of prey. <span class=\"searchmatch\">Owl</span>, <span class=\"searchmatch\">Owls</span> or <span class=\"searchmatch\">OWL</span> may also refer to: Fraternal Order of <span class=\"searchmatch\">Owls</span>, a fraternal order of the United States <span class=\"searchmatch\">Owl</span> Club (Harvard)","timestamp":"2017-10-25T23:41:30Z"},{"ns":0,"title":"Great grey owl","pageid":358689,"size":20254,"wordcount":2757,"snippet":"The great grey <span class=\"searchmatch\">owl</span> or great gray <span class=\"searchmatch\">owl</span> (Strix nebulosa) is a very large <span class=\"searchmatch\">owl</span>, documented as the world's largest species of <span class=\"searchmatch\">owl</span> by length. It is distributed","timestamp":"2017-10-23T04:19:32Z"},{"ns":0,"title":"Snowy owl","pageid":252110,"size":17438,"wordcount":1969,"snippet":"The snowy <span class=\"searchmatch\">owl</span> (Bubo scandiacus) is a large, white <span class=\"searchmatch\">owl</span> of the typical <span class=\"searchmatch\">owl</span> family. Snowy <span class=\"searchmatch\">owls</span> are native to Arctic regions in North America and Eurasia","timestamp":"2017-10-31T18:05:56Z"},{"ns":0,"title":"Web Ontology Language","pageid":248001,"size":41757,"wordcount":4380,"snippet":"The Web Ontology Language (<span class=\"searchmatch\">OWL</span>) is a family of knowledge representation languages for authoring ontologies. Ontologies are a formal way to describe taxonomies","timestamp":"2017-09-27T21:44:22Z"},{"ns":0,"title":"Barn owl","pageid":244511,"size":70869,"wordcount":7941,"snippet":"flight: white <span class=\"searchmatch\">owl</span>, silver <span class=\"searchmatch\">owl</span>, demon <span class=\"searchmatch\">owl</span>, ghost <span class=\"searchmatch\">owl</span>, death <span class=\"searchmatch\">owl</span>, night <span class=\"searchmatch\">owl</span>, rat <span class=\"searchmatch\">owl</span>, church <span class=\"searchmatch\">owl</span>, cave <span class=\"searchmatch\">owl</span>, stone <span class=\"searchmatch\">owl</span>, monkey-faced <span class=\"searchmatch\">owl</span>, hissing <span class=\"searchmatch\">owl</span>, hobgoblin","timestamp":"2017-11-04T01:43:51Z"},{"ns":0,"title":"Great horned owl","pageid":358093,"size":142193,"wordcount":19716,"snippet":"The great horned <span class=\"searchmatch\">owl</span> (Bubo virginianus), also known as the tiger <span class=\"searchmatch\">owl</span> (originally derived from early naturalists' description as the &quot;winged tiger&quot; or","timestamp":"2017-11-01T07:00:24Z"},{"ns":0,"title":"Owl City","pageid":23585683,"size":56754,"wordcount":5700,"snippet":"<span class=\"searchmatch\">Owl</span> City is an American electronica project created in 2007 in Owatonna, Minnesota; it is one of several projects by singer, songwriter and multi-instrumentalist","timestamp":"2017-11-04T18:48:37Z"},{"ns":0,"title":"Eastern screech owl","pageid":361206,"size":36661,"wordcount":4715,"snippet":"The eastern screech <span class=\"searchmatch\">owl</span> or eastern screech-<span class=\"searchmatch\">owl</span> (Megascops asio) is a small <span class=\"searchmatch\">owl</span> that is relatively common in Eastern North America, from Mexico to Canada","timestamp":"2017-10-22T20:26:20Z"},{"ns":0,"title":"Owl of Athena","pageid":3206672,"size":11552,"wordcount":1045,"snippet":"In Greek mythology, a little <span class=\"searchmatch\">owl</span> (Athene noctua) traditionally represents or accompanies Athena, the virgin goddess of wisdom, or Minerva, her syncretic","timestamp":"2017-10-06T10:34:30Z"}]}} 

私が追加する方法を、検索タグ(JSON)に私の新しいデータを追加しますPHPを使用して壮大な子供の新しいデータですか?

種類はこれを

+0

jsonファイル内の以前のすべてのデータが削除されますか? – javacoder

答えて

0

代わりについて:

$newer['query']['search'][20] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!"); 

あなたはちょうどこの操作を行うことができます。

$json_decode['query']['search'][20]['title'] = 'OWL Witch'; 
$json_decode['query']['search'][20]['snippet'] = 'One crazy owl!'; 

あなたは本当にあなたの変数$ json_decodeはしかし、それは非常に混乱だ呼び出すべきではありません

EDIT

私はちょうどあなたのjsonを見て、これはそれを行うための最良の方法ではないかもしれないことを認識しました。

$json_decode['query']['search'][] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!"); 

この意志:私はあなたが実際に既存の最後に新しいエントリを追加されて何をしたいのか、その後、より良い方法は次のようになります場合は、$json_decode['query']['search'][20]

に固有の情報を追加したいと想定していました配列に現在入っているエントリの数に関係なく、$json_decode['query']['search']配列の次の数値配列キーとして新しいエントリを追加します。

+0

結果を歓迎します。 – javacoder

+0

ようこそ。私はあなたのjsonを見てから私の答えを更新しました。私のオリジナルの答えはおそらくあなたがやろうとしていることを正確に行っていないことに気付きました。 – miknik

関連する問題