错误:将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错误的的更多相关文章

  1. 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 ...

  2. 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对象 ...

  3. iwebshop (: Cannot use object of type stdClass as array in)

    今天在PHP输出一个二维数组的时候,出现了“Fatal error: Cannot use object of type stdClass as array in……”. 这个二维数组是这样的: Ar ...

  4. PHP“Cannot use object of type stdClass as array”

    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...

  5. Fatal error: Cannot use object of type PHPExcel_RichText as array

    Fatal error: Cannot use object of type PHPExcel_RichText as array 上传导入Excel的时候会出现此问题,问题的原因是Excel表格中有 ...

  6. 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 ...

  7. 搭建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 ...

  8. MyBatis Generator报错:Cannot instantiate object of type

    [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate ( ...

  9. Unable to cast object of type 'System.Int32' to type 'System.Array'.

    x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...

随机推荐

  1. Python 不定参数函数

    1. 元组形式 def test1(*args): print('################test1################') print(type(args)) print(arg ...

  2. 什么是ground truth(GT)

    转自ground truth的含义 ground truth在不同的地方有不同的含义,下面是参考维基百科的解释,ground truth in wikipedia. 1.在统计学和机器学习中 在机器学 ...

  3. Hadoop生态圈-Hbase的rowKey设计原则

    Hadoop生态圈-Hbase的rowKey设计原则 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  4. java基础-引用数据类型之二维数组(Array)

    java基础-引用数据类型之二维数组(Array) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我们学习过了Java的一维数组,所谓的二维数组就是元素是一堆一维数组的数组,换 ...

  5. Vuex深入理解

    store下的index.js: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let store = new Vuex.St ...

  6. python3.6.4的importlib模块重载用法

    了解:模块的重载 考虑到性能的原因,每个模块只被导入一次,放入字典sys.module中,如果你改变了模块的内容,你必须重启程序,python不支持重新加载或卸载之前导入的模块, 有的同学可能会想到直 ...

  7. js事件的捕获和冒泡阶段

    讨论的主要是两个事件模型:IE事件模型与DOM事件模型 IE内核浏览器的事件模型是冒泡型事件(没有捕获事件过程),事件句柄的触发顺序是从ChildNode到ParentNode. <div id ...

  8. 样式缩写——css技巧(一)

    一.margin和padding缩写 例: .sample-margin1{ margin-top:15px; margin-right:20px; margin-bottom:12px; margi ...

  9. 轻松使用div模拟select下拉菜单

    没有办法,平时不是万不得已我是不喜欢去模拟各类控件的,一个是麻烦,二个是对性能也有些影响,还是原生的来的实在.老板昨天发话,必须模拟赶紧的,老外最喜欢简洁干净的风格,说的貌似都很在理的样子,业务部也是 ...

  10. ASP.NET mvc下在Controller下action的跳转方式

    在ASP.NET mvc下,action有多种挑战方式: return RedirectToAction("Index");//一个参数时在本Controller下 如果Redir ...