首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法访问合并数组的元素

无法访问合并数组的元素
EN

Stack Overflow用户
提问于 2015-05-27 08:49:37
回答 1查看 56关注 0票数 0

所以我有两个需要一起工作的对象,其中一个想法是合并它们。这样做了,使用了array_merge,但是现在我无法从合并的对象访问第二个对象,而且我被困住了。合并后出现的键看起来有点奇怪,我用错了吗?

代码语言:javascript
复制
$merged=array_merge_recursive((array)$post,(array)$image);

var_dump for $merged向我展示了以下内容:

代码语言:javascript
复制
array (size=10)
  0 => 
    object(Models\Post)[7]
      protected 'id' => string '17' (length=2)
      protected 'title' => string 'qwq' (length=3)
      protected 'body' => string 'wqwq' (length=4)
      protected 'views' => string '22' (length=2)
      protected 'imgId' => string '17' (length=2)
      protected '_attr_accessible' => 
        array (size=3)
          0 => string 'title' (length=5)
          1 => string 'body' (length=4)
          2 => string 'views' (length=5)
      protected '_validators' => 
        array (size=0)
          empty
      protected '_errors' => 
        array (size=0)
          empty
      protected '_valid' => boolean true
  '�*�id' => string '17' (length=2)
  '�*�path' => string '/media/' (length=7)
  '�*�name' => string 'col_right_callout.jpg' (length=21)
  '�*�idPost' => string '0' (length=1)
  '�*�description' => string 'wqeqw' (length=5)
  '�*�_attr_accessible' => 
    array (size=3)
      0 => string 'name' (length=4)
      1 => string 'path' (length=4)
      2 => string 'description' (length=11)
  '�*�_validators' => 
    array (size=0)
      empty
  '�*�_errors' => 
    array (size=0)
      empty
  '�*�_valid' => boolean true

如何从数组的第二部分访问路径或任何字段?$post含量

代码语言:javascript
复制
array (size=1)   0 => 
    object(Models\Post)[7]
      protected 'id' => string '17' (length=2)
      protected 'title' => string 'qwq' (length=3)
      protected 'body' => string 'wqwq' (length=4)
      protected 'views' => string '23' (length=2)
      protected 'imgId' => string '17' (length=2)
      protected '_attr_accessible' => 
        array (size=3)
          0 => string 'title' (length=5)
          1 => string 'body' (length=4)
          2 => string 'views' (length=5)
      protected '_validators' => 
        array (size=0)
          empty
      protected '_errors' => 
        array (size=0)
          empty
      protected '_valid' => boolean true

$image含量

代码语言:javascript
复制
object(Models\Image)[9]
  protected 'id' => string '17' (length=2)
  protected 'path' => string '/media/' (length=7)
  protected 'name' => string 'col_right_callout.jpg' (length=21)
  protected 'idPost' => string '0' (length=1)
  protected 'description' => string 'wqeqw' (length=5)
  protected '_attr_accessible' => 
    array (size=3)
      0 => string 'name' (length=4)
      1 => string 'path' (length=4)
      2 => string 'description' (length=11)
  protected '_validators' => 
    array (size=0)
      empty
  protected '_errors' => 
    array (size=0)
      empty
  protected '_valid' => boolean true
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-27 09:02:23

由于图像是对象,所以在将对象转换为数组时,受保护的私有属性具有特殊字符。

使用post,您将数组转换为数组。如果您执行类似于(数组)$post的操作,那么两个对象都会遇到相同的问题。

看看如何从其中获取数组:How to convert (cast) Object to Array without Class Name prefix in PHP?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30477501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档