Recursively converting objects to arrays
Posted: Wed Jul 30, 2008 11:27 am
Hi!
I've got a little problem. I have an object that has other objects as its properties. Here's an example:
Now I want to convert this object into an array. If I use get_object_vars, it converts the primary properties of the object into an array, but not the secondary ones (like [SmallImage]). I've tried various ways of converting it to an array, both recursively and non-recursively, but I just can't get it to work... My latest try is this:
No luck... can anyone please help?
Also, can anyone tell me whether there is a way in Zend Cache to write objects to the cache?
Thanks a lot in advance!
Jan
I've got a little problem. I have an object that has other objects as its properties. Here's an example:
Code: Select all
Zend_Service_Amazon_Item Object
(
[ASIN] => 0756629721
[DetailPageURL] => http://www.amazon.com/Mesopotamia-Eyewi ... 0756629721
[SalesRank] => 75655
[SmallImage] => Zend_Service_Amazon_Image Object
(
[Url] => Zend_Uri_Http Object
(
)
[Height] => 75
[Width] => 58
)
[MediumImage] => Zend_Service_Amazon_Image Object
(
[Url] => Zend_Uri_Http Object
(
)
[Height] => 160
[Width] => 124
)
[LargeImage] => Zend_Service_Amazon_Image Object
(
[Url] => Zend_Uri_Http Object
(
)
[Height] => 500
[Width] => 387
)
)
Code: Select all
$book_array = get_object_vars($book);
foreach ($book_array as &$entry) {
if (is_object($entry)) {
$entry = get_object_vars($entry);
}
}
Also, can anyone tell me whether there is a way in Zend Cache to write objects to the cache?
Thanks a lot in advance!
Jan