**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 ...
随机推荐
- 解决ImportError: cannot import name HTTPSHandler
/usr/local/python3.5/bin/pip3.5 install flask 的时候遇到了cannot import name HTTPSHandler 1. 原因在于openssl,o ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- codevs 1080 线段树练习 CDQ分治
codevs 1080 线段树练习 http://codevs.cn/problem/1080/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 一行N个 ...
- Lua 错误 收集
不存在的变量或者变量没有定义,提示错误 // :: [error] #: * lua entry thread aborted: runtime error: /opt/openresty/nginx ...
- idea 安装lombok 插件过程
一.作用 Lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具,bean,entity等类,绝大部分数据类类中都需要get.set.toStrin ...
- CloseableHttpClient与 CloseableHttpResponse应用
最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...
- Linux Ubuntu下安装配置mysql
检查系统中是否已经安装了mysql: sudo netstat -tap | grep mysql 安装mysql: sudo apt-get install mysql-server sudo ap ...
- windows下用python转换markdown到html
方法一: 安装markdown, pip install markdown, 安装好后,python -m markdown xxx.md -f xxx.html 方法二:安装markdown2, p ...
- bzoj 3790 神奇项链(Manacher,DP+BIT | 贪心)
[题意] 你可以产生一个回文串,也可以将两个串合并成一个串,问产生目标串需要的最少合并次数. [思路] 显然我们要先产生目标串中包含的极大回文字符串. Manacher求出每个位置可以向两边延伸的最长 ...
- [转]GCC常用参数详解
简介gcc and g++现在是gnu中最主要和最流行的c & c++编译器 .gcc/g++在执行编译工作的时候,总共需要以下几步:1.预处理,生成.i的文件[预处理器cpp]2.将预处理后 ...