将数组从json解码为PHP,并且无法使用键访问数组的元素 [英] decoding an array from json into PHP and unable to access elements of array using keys

查看:102
本文介绍了将数组从json解码为PHP,并且无法使用键访问数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似于以下内容的JSON:

I have some JSON similar to the following:

{"internalArray": {"201": "A", "202": "B", "5": "C", "46": "D"}, 
 "data": "ABCDEFG", 
 "data2": "TSXPIIF"}

我使用以下PHP代码对其进行解码:

I use the following PHP code to decode it:

$jsonOutput = json_decode($output);

我想从JSON数据访问"internalArray",因此我使用以下内容对其进行引用:

I would like to get access to the "internalArray" from the JSON data, so I reference it using the following:

$internalArray = $jsonOutput->{'internalArray'};

当我在$ internalArray上执行var_dump

When I do a var_dump on $internalArray

object(stdClass)#4 (4) 
{ ["201"]=> string(1) "A" 
     ["202"]=> string(1) "B" 
     ["5"]=> string(1) "C" 
     ["46"]=> string(1) "D" 
} 

我发现可以将其强制转换为数组,因此我执行了以下操作:

I figured out that I could cast this to an array, so I did the following:

$internalArray = (array) $jsonOutput->{'internalArray'};

但是,现在有了这个数组,我似乎无法使用类似的值来访问它

However, now that I have this array, I can't appear to access it using values like

$internalArray["202"], $internalArray["201"], etc.

当我尝试通过按键访问它时,它返回NULL.但是,当我有这样的代码时:

When I try to access it via the keys, it returns NULL. However, when I have code like this:

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $value;
}

它按预期打印出值,例如"202,A"等.

it prints out the values as expected, like "202,A", etc.

但是,如果我将其更改为

However, in the same code if I change it to

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $internalArray[$key];
}

它不起作用!

谁能解释为什么我不能使用键访问$ internalArray中的值?我在这里做错了什么吗?

Can anyone explain why I can't access the values in $internalArray using the keys? Am I doing something fundamentally wrong here?

推荐答案

如果需要关联数组,可以向PHP请求关联数组(请参阅

If you want an associative array, you can ask PHP for an associative array (see documentation for json_decode):

$jsonOutput = json_decode($output, true);

var_dump($jsonOutput['internalArray']);

产生:

array(4) {
  [201]=>
  string(1) "A"
  [202]=>
  string(1) "B"
  [5]=>
  string(1) "C"
  [46]=>
  string(1) "D"
}


回到您的问题,如果内部数组中的键不是数字键,则您的代码仍可以正常工作.这里发生的事情有点奇怪:PHP不允许您将数字字符串(例如:'201''46')用作数组的键.


Back to your issue, your code would still work if the keys in the internal array were not numeric. What is happening here is a little peculiar: PHP doesn't allow you to have numeric strings (eg: '201', '46') as keys for an array.

数字字符串键将转换为数字键.因此,当您执行$arr['201']时,PHP将改为查找$arr[201].但是,当您将对象转换为数组时,数组键看起来仍然是字符串(例如:$arr['201']).现在,实际的数组具有一个数字字符串键,但是每当您尝试访问它时,PHP都会查找一个int键,却找不到它,从而为您提供NULL.

Numeric strings keys will be converted to numbers keys instead. So when you do $arr['201'] PHP will look for $arr[201] instead. However, when you cast your object into an array, it looks like the array keys remain strings (eg: $arr['201']). Now the actual array has a numeric string key, but whenever you try to access it, PHP looks for an int key and never finds it, giving you NULL.

实际上,文档指出:

如果将对象转换为数组,则结果是一个数组,其元素是对象的属性.键是成员变量名称,几个值得注意的例外:整数属性不可访问; (...)

这篇关于将数组从json解码为PHP,并且无法使用键访问数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆