[英]Add data to JSON retrieved from URL, PHP
I'm using the Wikipedia web service for gaining results. 我正在使用Wikipedia Web服务获得结果。 I like to add more data to the retrieved JSON file in PHP.
我想将更多数据添加到PHP中检索到的JSON文件中。 I know you need to decode the JSON file before adding more data but not show how to add the data in the following format;
我知道您需要先解码JSON文件,然后再添加更多数据,但不展示如何以以下格式添加数据;
My PHP Code: 我的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 file: 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"}]}}
I like to add my new data in the search tag (JSON), how would I add new data in a grand child using PHP? 我想在搜索标签(JSON)中添加新数据,如何使用PHP在一个大孩子中添加新数据?
Kind regards 亲切的问候
Instead of this: 代替这个:
$newer['query']['search'][20] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");
You can just do this: 您可以这样做:
$json_decode['query']['search'][20]['title'] = 'OWL Witch';
$json_decode['query']['search'][20]['snippet'] = 'One crazy owl!';
You really shouldn't call your variable $json_decode though, it's very confusing 您确实不应该调用变量$ json_decode,这非常令人困惑
EDIT 编辑
I just had a look at your json and realised this might not be the best way to do it. 我只是看了看你的json,意识到这可能不是最好的方法。 I had assumed you wanted to add specific info to
$json_decode['query']['search'][20]
我以为您想向
$json_decode['query']['search'][20]
添加特定信息
If what you actually want to do is add a new entry to the end of the existing ones then a better method would be: 如果您真正想做的是在现有条目的末尾添加一个新条目,那么更好的方法是:
$json_decode['query']['search'][] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");
This will add your new entry as the next numerical array key in the $json_decode['query']['search']
array, regardless of how many entries are currently in the array. 这会将您的新条目添加为
$json_decode['query']['search']
数组中的下一个数字数组键,而不管当前数组中有多少个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.