我正在使用维基百科网络服务来获得结果。我想在PHP中为检索到的JSON文件添加更多数据。我知道您需要在添加更多数据之前解码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);
&GT;
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 "winged tiger" 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 :(得分: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,这是非常令人困惑的
修改强>
我刚刚看了你的json并意识到这可能不是最好的方法。我原以为您想要将特定信息添加到$json_decode['query']['search'][20]
如果您真正想要做的是在现有的条目末尾添加一个新条目,那么更好的方法是:
$json_decode['query']['search'][] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");
这会将新条目添加为$json_decode['query']['search']
数组中的下一个数字数组键,而不管数组中当前有多少条目。