您现在的位置是: 首页 > 后端开发 PHP 中list()出现Undefined offset: 0错误
PHP 中list()出现Undefined offset: 0错误
2020-05-20 【后端开发】 4759人已围观 10282次浏览
简介PHP 中list()出现Undefined offset: 0错误
在PHP中,list()主要用于在一次操作中,给一组变量赋值
比如:
$my_array = array('Dog','Cat','Horse');
list($a, $b, $c) = $my_array;
echo 'I have several animals, a '.$a.', a '.$b.' and a '.$c;
?>
正常输出,结果如下
I have several animals,a Dog,a Cat and a Horse.
但是,当数组不是数字数组,而是关联数组时,如
list($a,$b) = array('errno' => 0, 'errmsg' => "undefined");
结果就会报错,提示下面这样类似的错误
Notice: Undefined offset:0 in XXXXXX on line 1
出现这样的错误,就是因为list是从0开始索引数组,而不是通过关联数组来索引
当上面的代码修改为
list($a,$b) = array(0 => 0, 1 => "undefined");
就可以正常输出,但是当代码为
list($a,$b) = array(1 => 0, 2 => "undefined");
同样的,也是会提示下面类似的错误
Notice: Undefined offset:0 in XXXXXX on line 1
变量$a并没有被赋值,下标为1的元素给了变量@b,下标为0的元素并没有定义,导致$a为空,出现上面的错误提示。而下标为2的元素,没有赋值对象,留空
很赞哦! (0)
相关文章
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- Laravel Dingo/api 出现 The version given was unknown or has no registered routes.报错
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- 删除Git仓库所有历史提交记录,成为一个干净的仓库
- SpringBoot 启动测试时出现提示 Test class should have exactly one public zero-argument constructor
- Laravel项目出现could not be opened: failed to open stream: Permission denied
- SpringBoot 2.x Security security.basic.enabled=false 失效问题解决
- springboot引入mybatis-plus后出现ClassNotFoundException: org.mybatis.logging.LoggerFactory
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- SqlServer 优化技巧
点击排行
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR DISABLE You can't write or read against a disable instance
- Debian apt 使用国内镜像
- RocketMQ 出现 sendDefaultImpl call timeout 问题
- 类 BASE64Decoder 程序包 sun.misc 找不到符号
- SpringBoot @NotBlank 不生效问题
- 记一次 Mybatis-Plus 自动填充无效问题解决
- SpringBoot 2.x 文件上传出现 The field file exceeds its maximum permitted size of 1048576 bytes
- nuxt 项目完整部署方案