错误:将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. composer安装第三方库出现需要认证信息等原因

    最近,在学习使用thinkcmf的时候,使用composer安装第三方类库,遇到了需要输入验证码的问题,援引https://laravel-china.org/topics/17893该链接中的讨论, ...

  2. SystemV-IPC

    这里记录的三种SystemV-IPC包括(消息队列,信号量以及共享内存) 1:标识符和键值 键值(key_t) : IPC结构的外部名(所谓外部名就是各用户进程可获得并操作的,通过它使用XXXget获 ...

  3. word2vec原理CBOW与Skip-Gram模型基础

    转自http://www.cnblogs.com/pinard/p/7160330.html刘建平Pinard word2vec是google在2013年推出的一个NLP工具,它的特点是将所有的词向量 ...

  4. RACCommand中的信号

    示例: RACSignal* textSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscri ...

  5. 自定义UITableViewCell的方法

    1.纯XIB/storyboard自定义.对应一个Controller的storyboard上拖拽出一个自定义Cell,并加上ReuseIdentifitor 2.纯代码自定义,通过在contentV ...

  6. Windows 下安装和配置 MongoDB(二)

    因为电脑重新安装了系统,所以要重新安装开发环境,按照之前写过的一篇博客介绍的步骤进行安装,发现报了一些错误,下面是遇到的问题和解决方法: 首先下载安装就不多说了,下载地址:https://www.mo ...

  7. 实现asp.net的文件压缩、解压、下载

    很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...

  8. 【bzoj4942】[Noi2017]整数 压位+线段树

    题目描述 P 博士将他的计算任务抽象为对一个整数的操作. 具体来说,有一个整数 $x$ ,一开始为0. 接下来有 $n$ 个操作,每个操作都是以下两种类型中的一种: 1 a b :将 $x$ 加上整数 ...

  9. Golang实现mysql读库映射成Map【Easy】

    这个类库灵感来源于.net的dbHelper类,因为其简单易用,现在go的driver必须使用对象映射,这让人火大不爽,不能实现灵活的Map,在Key经常变动的业务场景里面非常不爽,我还是喜欢直接写s ...

  10. keras LSTM中间的dropout

    TM有三个 model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2)) 第一个dropout是x和hidden之间的dropout,第二个是hid ...