laravel 处理传参时报错 Illegal string offset


ErrorException in TestController.php line 1670:

Illegal string offset 'id'


于是乎我接收打印了了下数据:

$input = Request::input();


dd($input);


结果:

array(3) { [0]=> array(14) { ["id"]=> int(85)  } [1]=> array(14) { ["id"]=> int(86) } ["s"]=> string(37) “/Test/save”}

int(85)

int(86)


数据里面居然多了个元素s, 数组长度变成了3,那原因找到了,第三个参数是字符串不是数组,处理到第三个元素时报错了。

示例代码:

<?php

$abc = array('a'=>1,'b' => 2, 'c' => 3);

 

echo $abc['b'];

echo $abc[‘d’];  //Undefined index: d


$abc = "new abc”;
 
 

echo $abc['b']; // Illegal string offset 'b'

?>

分析下:

Illegal string offset b 和  Undefined index: d 区别:

前者Illegal string offset b  是字符串,肯定不能当数组使用。

后者Undefined index: d 是数组中不含有这个索引元素。


解决办法:

使用is_array  和 isset函数前置判断是否是数组和是否含有此索引,

is_array($abc) && isset($abc[‘b’])  ,二者也可只用其一根据情况选择使用。






本文链接二维码可以保存在本地:保存

| 浏览 (6501) | (0) | (0) | | 2017-09-28 18:19:00 |


相关文章

  1. Mac安装Swoole扩展phpize 时 Cannot find autoconf 解决方法
  2. mac下安装swoole扩展报错PHP Startup Unable to load dynamic library
  3. Laravel 数据库事务
  4. PHP贷款等额本息、等额本金计算月供还款计划公式
  5. Laravel 关闭过滤排除部分路由VerifyCsrfToken 验证
  6. Laravel Horizon监控Redis队列queue