[英]How to parse a json in php
我在php中解析下面的json
{
"responseHeader":{
"status":0,
"QTime":22,
"params":{
"fl":"title,id",
"indent":"true",
"q":"einstein",
"hl.simple.pre":"<em>",
"hl.simple.post":"</em>",
"wt":"json",
"hl":"true",
"rows":"3"}},
"response":{"numFound":63,"start":0,"docs":[
{
"id":"1",
"title":"Albert Einstein"},
{
"id":"2088",
"title":"Nationalism"},
{
"id":"1551",
"title":"Dean Koontz"}]
},
"highlighting":{
"1":{
"text":[" for school exam September The Collected Papers of Albert <em>Einstein</em> Vol Doc s Unthinking for authority"]},
"2088":{
"text":[" in a letter to Alfred Kneser June Doc in The Collected Papers of Albert <em>Einstein</em> Vol Nationalism"]},
"1551":{
"text":[" changes since meeting Travis Did you get the leash on him yet <em>Einstein</em> Part Chapter Nora s query during"]}}}
使用json_decode並循環遍歷結果數組我可以獲得docs部分中的各個元素,
foreach ($myArray['response']['docs'] as $doc) {
echo $doc['id'] . "<br/>";
echo $doc['title'] . "<br/>";
}
我現在試圖弄清楚從這個json的突出顯示部分獲取值。 我想在突出顯示部分中獲取文本字段並將其存儲在數組中。
"highlighting":{
"1":{
"text":[" for school exam September The Collected Papers of Albert <em>Einstein</em> Vol Doc s Unthinking for authority"]},
"2088":{
"text":[" in a letter to Alfred Kneser June Doc in The Collected Papers of Albert <em>Einstein</em> Vol Nationalism"]},
"1551":{
"text":[" changes since meeting Travis Did you get the leash on him yet <em>Einstein</em> Part Chapter Nora s query during"]}}}
數組應該是這樣的,
"1" => " for school exam September The Collected Papers of Albert <em>Einstein</em> Vol Doc s Unthinking for authority"
"2088" => " in a letter to Alfred Kneser June Doc in The Collected Papers of Albert <em>Einstein</em> Vol Nationalism"
怎么做到這一點? 有沒有辦法將docs的id元素映射到突出顯示部分中指定的數字?
你可以試試這個( 例子 )
$myArray = json_decode($json, true);
$highlighting = array();
foreach($myArray['highlighting'] as $key => $value)
{
$highlighting[$key] = $value['text'][0];
}
結果:
Array ( [1] => for school exam September... [2088] => in a letter to Alfred ... [1551] => changes since meeting ... )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.