[英]How to access third level value in multidimensional mixed associative and numeric array
我正在使用下面的数组,并想问我应该如何访问/引用“ match_id”和“ match_comp_ID”中的值?
我需要以两种方式引用它:问题1:首先在foreach语句中。 这已经在下面得到回答:
foreach $jason_a['matches'] as $match {
echo $match['match_id']
echo $match['match_comp_id']
}
问题2:我想通过使用我将通过usort调用的sort函数,用相同的两个键对上述输出进行排序:
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");
在sort函数中使用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
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type
。 我认为这是我的问题。 但是,如果数组不能用作键,那么如何访问此键值?
foreach ($json_a['matches'] as $match) {
// do something with $match['match_id'] and $match['match_comp_id']
}
对于问题的第2部分,您确实希望将“ matches”子数组传递给您的sort函数:
$matches = $json_a['matches'];
usort($matches, 'cmp');
// now the $matches array should be sorted according to rules in function cmp()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.