【问题标题】:how to find an element in multidimensional array如何在多维数组中找到一个元素
【发布时间】:2010-11-20 20:38:16
【问题描述】:

我有以下疑问:

$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'];

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

【问题讨论】:

  • 能否添加print_r($resultarray) 以便我们查看数组输出的格式?
  • 你试过做一个 print_r($resultarray);在最后一行之后检查数组是如何返回的?这会让你更清楚。
  • 您对“景点”的拼写错误是复制/粘贴错误,还是存在于您的实际代码中?

标签: php arrays json


【解决方案1】:
$result = $resultarray["q1"]["result"]["tourist_attractions"]['/common/topic/image'][0]['guid'];

【讨论】:

  • 最后的主要问题是“景点”这个词的拼写错误。
【解决方案2】:

第一个猜测?你拼错了“景点”:

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

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 2021-12-28
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多