简体   繁体   English

PHP:对象类型强制转换为数组后,奇怪的数组行为

[英]PHP: Strange Array Behaviour After Object Type Casting to Array

When you do array type-casting of json_decode d value (with $assoc = false ), PHP creates an array with string indices: 当您对json_decode d值进行array类型转换(使用$assoc = false )时,PHP将创建一个具有字符串索引的数组:

$a = (array)json_decode('{"7":"value1","8":"value2","9":"value3","13":"value4"}');

var_export($a);

//array (
//  '7' => 'value1',
//  '8' => 'value2',
//  '9' => 'value3',
//  '13' => 'value4',
//)

And for some reason these indices are not accessible: 由于某些原因,这些索引不可访问:

var_dump(isset($a[7]), isset($a['7']));

//false
//false

When you try to create the same array by PHP itself, it is being created with numeric indices (string are automatically converted), and values are accessible using both strings and numbers: 当您尝试通过PHP本身创建相同的数组时,正在使用数字索引创建它(自动转换字符串),并且可以使用字符串和数字访问值:

$c = array('7' => 'value1', '8' => 'value2', '9' => 'value3','10' => 'value4');

var_export($c);

var_dump(isset($c[7]), isset($c['7']));

//array (
//  7 => 'value1',
//  8 => 'value2',
//  9 => 'value3',
//  13 => 'value4',
//)
//
//true
//true

Does anybody know what is going on here? 有人知道这是怎么回事吗? Is it some bug of older PHP versions (the issue seems to be fixed on PHP version >= 7.2, but I can't find anything related in changelog )? 这是旧PHP版本的一些错误吗(问题似乎已在PHP版本> = 7.2上解决,但我在changelog中找不到任何相关内容)?

Here's the demo of what is going on: https://3v4l.org/da9CJ . 这是正在进行的演示: https : //3v4l.org/da9CJ

This seems to be related to bug #61655 fixed in 7.2.0: 这似乎与7.2.0中修复的错误#61655有关:

in a object property lookup by name always in string, but in array numeric string(like "22200" ) key will transform to numeric but not a string anymore. 在对象属性中按名称查找总是在字符串中进行,但是在数组数字字符串中(例如“ 22200”),键将转换为数字,但不再转换为字符串。 when conversion internal HashTable did't changed so after conversion, key lookup will fail. 当转换内部HashTable不变时,转换后,键查找将失败。

Clarified: $a["2000"] is always interpreted as $a[2000] , but (array) failed to cast object string keys to numbers. 澄清: $a["2000"]始终解释为$a[2000] ,但是(array)无法将对象字符串键转换为数字。 So the array contained string numeric indices, but the array syntax' automatic casting prevented those from being accessible. 因此,数组包含字符串数字索引,但是数组语法的自动强制转换阻止了这些字符串的访问。

add TRUE to json_decode() TRUE添加到json_decode()

<?php
$a = json_decode('{"7":"value1","8":"value2","9":"value3","13":"value4"}',TRUE);

var_export($a);

var_dump(isset($a[7]), isset($a['7']));

https://3v4l.org/YuF9B https://3v4l.org/YuF9B

add TRUE to json_decode() is possible, but it will couse to recode everything. 可以在json_decode()中添加TRUE,但是它将重新编码所有内容。

Becouse you have to change the access to the variables. 因为您必须更改对变量的访问。

if your json look like this: 如果您的json看起来像这样:

$return = '{"status":"ok","message":"","code":"200","data":{"1234":{"sid":1,"name":"foo"},"4321":{"sid":2,"name":"bar"}}}';

on: 上:

$json_data = json_decode($return, true);
$data = $json_data['data'];

you can loop the $data and have to access the values as array: $data[0]['name'] ... 您可以循环$ data并以数组形式访问值:$ data [0] ['name'] ...

on: 上:

$json_data = json_decode($return);
$data = (array) $json_data->data;

you can loop the $data and have to access the values as objects: $data[0]->name ... 您可以循环$ data并以对象的形式访问值:$ data [0]-> name ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM