在访问数组元素所存在不确定的偏移 [英] Undefined offset while accessing array element which exists

查看:179
本文介绍了在访问数组元素所存在不确定的偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组和PHP,当我把它打印出来,我可以看到我需要访问的价值,但是当我试图通过他们的主要访问这些我得到一个PHP的通知。我打印的阵列的print_r

 阵列

    [207] => SDF
    [210] => SDF

当我尝试使用我得到一个未定义的偏移通知索引来访问数组。这里是我的code:

 的print_r($输出);
回声$输出[207]; //未定义偏移
回声$输出[207]; //未定义偏移

$输出数组是一个电话来 array_diff_key 并是结果通过HTTP POST请求输入最初是作为JSON。

array_keys 给了我以下内容:

 阵列

   [0] => 207
   [1] => 210


在回应评论:

的var_dump(键($输出)); 输出:


 字符串(3)207


的var_dump(使用isset($输出[键($输出))); 输出:


 布尔(假)



解决方案

请参阅本节上的的 PHP手册


  

键是成员变量名,有几个明显的例外: 整数属性不可访问;私有变量有ppended的变量名的类名$ P $;受保护的变量有ppended的变量名'*'$ P $。


当从PHP对象转换成一个数组,整数数组键内部存储为字符串。当PHP您访问数组元素或正常使用数组,包含有效整数键将被自动转换为整数。内部存储为一个字符串的整数不可访问的关键。

请注意区别:

  $ X =(阵列)json_de code({207:测试}');
后续代码var_dump(键($ X)); //字符串(3)207后续代码var_dump($ X);
//阵列(1){
// [207] =>
//串(4)测试
//}
$ Y ['207'] ='测试';
后续代码var_dump(键($ Y)); // INT(207)后续代码var_dump($ Y);
//阵列(1){
// [207] =>
//串(4)测试
//}

的print_r 在这两个阵列提供了相同的输出,但与 的var_dump你可以看到不同之处。

下面是一些code重现你确切的问题:

  $输出=(阵列)json_de code({207:自卫队,210:自卫队}');的print_r($输出);
回声$输出[207];
回声$输出[207];

和简单的解决方法是在真正 json_de $ C $传递对于可选的 assoc命令的说法,C 来指定要数组不是一个对象:

  $输出= json_de code({207:自卫队,210:自卫队},真正的);的print_r($输出);
回声$输出[207];
回声$输出[207];

I have an array and PHP and when I print it out I can see the values I need to access, but when I try accessing them by their key I am getting a PHP Notice. I printed the array with print_r:

Array
(
    [207] => sdf
    [210] => sdf
)

When I try to access the array using the index I get an undefined offset notice. Here is my code:

print_r($output); 
echo $output[207];   // Undefined Offset
echo $output["207"]; // Undefined Offset

The $output array is the result of a call to array_diff_key and is input originally as JSON through an HTTP POST request.

array_keys gives me the following:

Array
(
   [0] => 207
   [1] => 210
)


In response to the comments:

var_dump(key($output)); outputs:

   string(3) "207"

var_dump(isset($output[key($output)])); outputs:

   bool(false)

解决方案

See this section on converting an object to an array in the PHP Manual:

The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name.

When converting to an array from an object in PHP, integer array keys are stored internally as strings. When you access array elements in PHP or use an array normally, keys that contain valid integers will be converted to integers automatically. An integer stored internally as a string is an inaccessible key.

Note the difference:

$x = (array)json_decode('{"207":"test"}');
var_dump(key($x));  // string(3) "207"

var_dump($x);
// array(1) {
//   ["207"]=>
//   string(4) "test"
// }


$y['207'] = 'test';
var_dump(key($y));  // int(207)

var_dump($y);
// array(1) {
//   [207]=>
//   string(4) "test"
// }

print_r on both those arrays gives identical output, but with var_dump you can see the differences.

Here is some code that reproduces your exact problem:

$output = (array)json_decode('{"207":"sdf","210":"sdf"}');

print_r($output);
echo $output[207];
echo $output["207"];

And the simple fix is to pass in true to json_decode for the optional assoc argument, to specify that you want an array not an object:

$output = json_decode('{"207":"sdf","210":"sdf"}', true);

print_r($output);
echo $output[207];
echo $output["207"];

这篇关于在访问数组元素所存在不确定的偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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