访问存在的数组元素时未定义的偏移量 [英] Undefined offset while accessing array element which exists

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

问题描述

我有一个数组和 PHP,当我打印出来时,我可以看到我需要访问的值,但是当我尝试通过它们的键访问它们时,我收到了一个 PHP 通知.我用 print_r 打印了数组:

数组([207] =>自卫队[210] =>自卫队)

当我尝试使用索引访问数组时,我收到一条未定义的偏移通知.这是我的代码:

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

$output 数组是调用 array_diff_key 的结果,最初是通过HTTP POST 请求.

array_keys 给了我以下内容:

数组([0] =>207[1] =>210)

<小时>

回应评论:

var_dump(key($output)); 输出:

<块引用>

 string(3) "207"

var_dump(isset($output[key($output)])); 输出:

<块引用>

 bool(false)

解决方案

请参阅 PHP 手册:

<块引用>

键是成员变量名,有几个值得注意的例外:整数属性不可访问;私有变量在变量名前加上了类名;受保护的变量在变量名前有一个*".

在 PHP 中从对象转换为数组时,整数数组键在内部存储为字符串.当您在 PHP 中访问数组元素或正常使用数组时,包含有效整数的键将自动转换为整数.内部存储为字符串的整数是不可访问的键.

注意区别:

$x = (array)json_decode('{"207":"test"}');var_dump(key($x));//字符串(3) "207"var_dump($x);//数组(1) {//[207"]=>//string(4) "测试"//}$y['207'] = '测试';var_dump(key($y));//整数(207)var_dump($y);//数组(1) {//[207]=>//string(4) "测试"//}

print_r 在这两个数组上给出相同的输出,但是 var_dump 你可以看到不同之处.

这里有一些代码可以重现您的确切问题:

$output = (array)json_decode('{"207":"sdf","210":"sdf"}');打印_r($输出);回声$输出[207];回声 $output["207"];

简单的解决方法是将 true 传递给 json_decode 用于可选的 assoc 参数,指定你想要一个数组而不是一个对象:

$output = json_decode('{"207":"sdf","210":"sdf"}', true);打印_r($输出);回声$输出[207];回声 $output["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天全站免登陆