【问题标题】:How to access third level value in multidimensional mixed associative and numeric array如何访问多维混合关联和数值数组中的第三级值
【发布时间】:2015-05-17 01:50:57
【问题描述】:

我正在使用下面的数组并想问我应该如何访问/引用“match_id”和“match_comp_ID”中的值?

我需要通过两种方式引用它: 问题 1:首先在 foreach 语句中。答案如下:

foreach $jason_a['matches'] as $match {
echo $match['match_id']
echo $match['match_comp_id']
}

问题 2:我想通过使用我将通过 usort 调用的排序函数按这两个键对上面的输出进行排序:

function cmp($a, $b)
  {
    // sort by match_id
        $retval = strnatcmp(substr($b->match_id,0,10), substr($a->match_id,0,10));
    // if identical, sort by match_comp_id
  if(!$retval)     $retval = strnatcmp($a->match_comp_id, $b->match_comp_id);
    return $retval;

  }

usort($json_a, "cmp");

在排序函数中使用match_id 或$json['match_id] 格式不起作用。我不知道要搜索什么。

数组是:

array(4) { 

 ["APIRequestsRemaining"]=> int(920) 
 ["matches"]=> array(3) { 

                [0]=> array(3) { 
                        ["match_id"]=> string(7) "1999477" 
                        ["match_static_id"]=> string(7) "1755895" 
                        ["match_comp_id"]=> string(4) "1204" } 

                [1]=> array(3) { 
                        ["match_id"]=> string(7) "1999478" 
                        ["match_static_id"]=> string(7) "1755891" 
                        ["match_comp_id"]=> string(4) "1204" } 

                [2]=> array(3) { 
                        ["match_id"]=> string(7) "1999479" 
                        ["match_static_id"]=> string(7) "1755894" 
                        ["match_comp_id"]=> string(4) "1204" } 
         } 
 ["Action"]=> string(5) "today" 
 ["Params"]=> array(4) { 
                ["Action"]=> string(5) "today" 
                ["APIKey"]=> string(31) "xxxx-xxxx-xxxx-xxxx" 
                ["OutputType"]=> string(4) "JSON" 
                ["comp_id"]=> string(4) "1204" 
         }

php 手册指出: Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type。我认为这是我的问题。但是如果数组不能作为key,那么如何访问这个key-value呢?

【问题讨论】:

  • .. foreach (['matches'] as $x){ echo $x['match_id'] ..
  • 谢谢,回答问题 1。
  • 请一次只发布一个问题
  • 抱歉,不知道是继续还是开始新的最好。按照我认为它们相关的方式进行了编辑。

标签: php arrays json multidimensional-array


【解决方案1】:
foreach ($json_a['matches'] as $match) {
    // do something with $match['match_id'] and $match['match_comp_id']
}

对于您问题的第 2 部分,您确实想将“匹配”子数组传递给您的排序函数:

$matches = $json_a['matches'];
usort($matches, 'cmp');
// now the $matches array should be sorted according to rules in function cmp()

【讨论】:

  • 谢谢,完美回答了我的问题 1。
  • 使用 $matches = $json_a['matches']; usort($matches, 'cmp'); foreach ($jason_a['matches'] as $match) {echo $match['match_id'];} 我收到错误:尝试获取非对象的属性
  • 抱歉 - 发布了错误的数据:$retval = strnatcmp(substr($b->match_id,0,10), substr($a->match_id,0,10));还有 if(!$retval) $retval = strnatcmp($a->match_comp_id, $b->match_comp_id);
  • 您正在使用带有-> 的对象属性访问语法,但最终填充$a 和$b 的每个匹配都是一个数组。改用数组表示法:$a['match_id']
  • 已修改。现在没有错误但没有排序。将仔细检查所有内容并报告。
猜你喜欢
  • 2010-10-27
  • 2013-01-07
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多