**PHP错误Cannot use object of type stdClass as array in错误的
错误:将PHP对象类型当做了PHP数组 解决方法:用对象操作符->
今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”。
这个二维数组是这样的:
Array (
[0] => stdClass Object (
[id] => 1
[title] => 首页招聘
[size] => 297*140
[pic] => ./upload/20130302093535.jpg
[state] => 0 )
[1] => stdClass Object (
[id] => 2
[title] => 首页领队
[size] => 297*140
[pic] => ./upload/20130302093443.jpg
[state] => 0 )
)
输出开始写的方法是:$pic[0][title]
结果就出现上面的错误。
其实,数组中是返回的是一个对象,不能直接用[]来显示,正确的输出方法是:$pic[0]->title (不用加引号 )
错误的:
foreach($user_list as $user_key => $user_value){
foreach ($data as $data_key => $data_value){
if($user_list[$user_key]['user_mobile'] === $data_key){
$user_list[$user_key]['contacts_username'] = $data_value;
break;
}
}
}
正确:
$user_list =$this->m_user->match_user_mobile($column_str, $mobile_array, $page_num, $page_size);
//遍历$user_list,追加通讯录的用户名
//遍历二维数组
foreach($user_list as $user_key => $user_value){
foreach ($data as $data_key => $data_value){
if($user_list[$user_key]->user_mobile === $data_key){
$user_list[$user_key]->contacts_username = $data_value;
break;
}
}
} --------------------------------------------
public function match_user_mobile($column_str, $mobile_array, $page_num, $page_size)
{
$this->db->select($column_str);
$this->db->from('xm_user');
$this->db->where_in('user_mobile', $mobile_array);
//$this->db->limit(10, 20); 生成: LIMIT 20, 10 (仅限MySQL中。其它数据库有稍微不同的语法)
$this->db->limit($page_size, ($page_num - 1)*$page_size);
$query = $this->db->get(); //var_dump($query); 测试
//row_array取一行数据;result_array取多行数据,返回关联数组;result返回对象数组
return $query->result_array(); }
总结:
如果数据库用result返回,那么就是PHP对象数组,需要用对象操作符->
如果是result_array返回,那么就是PHP关联数组,用[]即可
**PHP错误Cannot use object of type stdClass as array in错误的的更多相关文章
- PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...
- PHP json_decode object时报错Cannot use object of type stdClass as array
PHP json_decode object时报错Cannot use object of type stdClass as array php再调用json_decode从字符串对象生成json对象 ...
- iwebshop (: Cannot use object of type stdClass as array in)
今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”. 这个二维数组是这样的: Ar ...
- PHP“Cannot use object of type stdClass as array”
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...
- Fatal error: Cannot use object of type PHPExcel_RichText as array
Fatal error: Cannot use object of type PHPExcel_RichText as array 上传导入Excel的时候会出现此问题,问题的原因是Excel表格中有 ...
- spring boot 之 错误:SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap'
这个错误我也见过很多次了,今天终于理解了其出现的原因. 错误是这样的: 2017-11-23 18:05:39.504 ERROR 4092 --- [nio-8080-exec-3] o.a.c.c ...
- 搭建Turbine时,报错误:Property or field 'default' cannot be found on object of type 'com.netflix.appinfo.InstanceInfo'
Spring Boot + Eureka Server + Hystrix with Turbine: empty turbine.stream 配置的时候遇到了问题: Property or fie ...
- MyBatis Generator报错:Cannot instantiate object of type
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate ( ...
- Unable to cast object of type 'System.Int32' to type 'System.Array'.
x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...
随机推荐
- Spss22安装与破解步骤
Spss22安装与破解教程 向师姐请教了一些学术问题,哈哈说的有点大,不过真的很感谢,学到了很多,少走了很多弯路. 1.下载安装包 可以去IBM官网.人大论坛等网站下载,全部文件应包括spss22安装 ...
- etcd启用https服务
目录 cfssl相关工具下载 生成etcd所需要的ssl证书 生成ca证书 生成etcd服务端证书 生成etcd客户端证书 修改etcd集群配置文件 重启etcd集群 验证集群健康情况 关于etcd的 ...
- Linux操作系统常见安装方式
Linux操作系统常见安装方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在window操作系统安装程序只需要点点鼠标就能搞定的事情,但是在Linux操作系统中,尤其是字符终端 ...
- npm 5.4.2 更新后就不能用了
今天刚,npm run dev 就出现更新提示,没多想就更了, 更新用了49S,下来npm 的所以命令包一个semer的插件 ... 最后下载新node 8.5覆盖安装, 就解决了, node 8.5 ...
- 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解
关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...
- [USACO09NOV]硬币的游戏A Coin Game
https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...
- Linux安装配置SVN服务器
1. 安装SVN服务器: 检查是否已安装 # rpm -qa subversion 安装SVN服务器 # yum install httpd httpd-devel subversion mod_da ...
- 使用abcpdf将html转换成pdf文件
ABCpdf.NET使用介绍 最新做一个项目需要生成pdf文档以供打印,研究决定使用abcpdf这款组件,先针对其使用方法做一个简单的总结介绍以给有需要的朋友做参考. 一. ABCpdf.NET简单介 ...
- ASP.NET中常用输出JS脚本的类(来自于周公博客)
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- bzoj 2342 [Shoi2011]双倍回文(manacher,set)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题意] 求出形如w wR w wR的最长连续子串. [思路] 用manache ...