Hi
Can anyone tell me if there is an easy way to convert an object into an array.
I currently have the following object:-
As you can see the object contains various items some of which are arrays, I wish the objects within these arrays to be converted into arrays as well (ie it should recursively go through all arrays and objects and convert the content into arrays if need be).PHP Code:stdClass Object
(
[documentFiltering] =>
[searchComments] =>
[estimatedTotalResultsCount] => 17900000
[estimateIsExact] =>
[resultElements] => Array
(
[0] => stdClass Object
(
[summary] =>
[URL] => http://www.test.com/
[snippet] => Provides extranet privacy to clients making a range of <b>tests</b> and surveys available <br> to their human resources departments. Companies can <b>test</b> prospective and <b>...</b>
[title] => <b>Test</b> Central Home
[cachedSize] => 39k
[relatedInformationPresent] => 1
[hostName] =>
[directoryCategory] => stdClass Object
(
[fullViewableName] =>
[specialEncoding] =>
)
[directoryTitle] =>
)
[1] => stdClass Object
(
[summary] =>
[URL] => http://www.bandwidthplace.com/speedtest/
[snippet] => Personal <b>Test</b>. <b>Test</b> the speed of your Internet connection Free up to 3 times a <br> month Purchase a subscription for. Up to 1000 <b>tests</b> per month; Personal <b>test</b> <b>...</b>
[title] => Bandwidth Speed <b>Test</b>
[cachedSize] => 15k
[relatedInformationPresent] => 1
[hostName] =>
[directoryCategory] => stdClass Object
(
[fullViewableName] =>
[specialEncoding] =>
)
[directoryTitle] =>
)
[2] => stdClass Object
(
[summary] =>
[URL] => http://www.innergeek.us/geek.html
[snippet] => Geeks everywhere have taken the Original Geek <b>Test</b>, a comprehensive 507-point <br> survey of how geeky you are. Possible ranks include Geekish Tendencies, Geek, <b>...</b>
[title] => Geek Out with the Original Geek <b>Test</b>
[cachedSize] => 10k
[relatedInformationPresent] => 1
[hostName] =>
[directoryCategory] => stdClass Object
(
[fullViewableName] =>
[specialEncoding] =>
)
[directoryTitle] =>
)
)
[searchQuery] => test
[startIndex] => 1
[endIndex] => 3
[searchTips] =>
[directoryCategories] => Array
(
)
[searchTime] => 0.026511
)
I have created the following code:-
but it does not convert the objects within the array probably for some silly reason that I cannot currently see.PHP Code:function object_2_array($result)
{
$array = array();
foreach ($result as $key=>$value)
{
if (is_object($value))
{
$array[$key]=object_2_array($value);
}
if (is_array($value))
{
$array[$key]=object_2_array($value);
}
else
{
$array[$key]=$value;
}
}
return $array;
}





glad it worked with no probs for you. 
Bookmarks