如何在多维数组中查找元素

时间:2010-11-20 20:38:17

标签: php arrays json

我有以下查询:

$simplequery = array('type'=>'/travel/travel_destination',
                     'id'=>$_POST["hcity"],
                     'name' => null,
                     'tourist_attractions' => array(
                         array('/common/topic/article'=>array(array('guid'=>null)),
                               '/common/topic/image'=>array(array('guid'=>null))
                         )
                     )
);

$queryarray = array('q1' => array('query' => $simplequery));
$jsonquerystr = json_encode($queryarray);
//echo $jsonquerystr;
#run the query 
$apiendpoint = "http://www.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
$temp = str_replace("#", "", $jsonresultstr);
$resultarray = json_decode($temp, true);

我想采用'/ common / topic / image'数组的guid字符串。这是一个多维数组,我无法找到如何做到这一点。 我试过写作

$result = $resultarray["q1"]["result"]["tourist_atractions"]['/common/topic/image'];

但它不起作用。你能帮我么?感谢

2 个答案:

答案 0 :(得分:0)

$result = $resultarray["q1"]["result"]["tourist_attractions"]['/common/topic/image'][0]['guid'];

答案 1 :(得分:0)

首先猜?你拼错了“景点”:

$resultarray["q1"]["result"]["tourist_attractions"]['/common/topic/image'];