所以我有两个需要一起工作的对象,其中一个想法是合并它们。这样做了,使用了array_merge,但是现在我无法从合并的对象访问第二个对象,而且我被困住了。合并后出现的键看起来有点奇怪,我用错了吗?
$merged=array_merge_recursive((array)$post,(array)$image);var_dump for $merged向我展示了以下内容:
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含量
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含量
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发布于 2015-05-27 09:02:23
由于图像是对象,所以在将对象转换为数组时,受保护的私有属性具有特殊字符。
使用post,您将数组转换为数组。如果您执行类似于(数组)$post的操作,那么两个对象都会遇到相同的问题。
看看如何从其中获取数组:How to convert (cast) Object to Array without Class Name prefix in PHP?
https://stackoverflow.com/questions/30477501
复制相似问题