【发布时间】: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