Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert multidimensional objects to array

I'm using amazon product advertising api. Values are returned as a multidimensional objects.

It looks like this:

object(AmazonProduct_Result)#222 (5) {   ["_code":protected]=>   int(200)   ["_data":protected]=>   string(16538)  array(2) {     ["IsValid"]=>     string(4) "True"     ["Items"]=>     array(1) {       [0]=>       object(AmazonProduct_Item)#19 (1) {         ["_values":protected]=>         array(11) {           ["ASIN"]=>           string(10) "B005HNF01O"           ["ParentASIN"]=>           string(10) "B008RKEIZ8"           ["DetailPageURL"]=>           string(120) "http://www.amazon.com/Case-Logic-TBC-302-FFP-Compact/dp/B005HNF01O?SubscriptionId=AKIAJNFRQCIJLTY6LDTA&tag=*********-20"           ["ItemLinks"]=>           array(7) {             [0]=>             object(AmazonProduct_ItemLink)#18 (1) {               ["_values":protected]=>               array(2) {                 ["Description"]=>                 string(17) "Technical Details"                 ["URL"]=>                 string(217) "http://www.amazon.com/Case-Logic-TBC-302-FFP-Compact/dp/tech-data/B005HNF01O%3FSubscriptionId%3DAKIAJNFRQCIJLTY6LDTA%26tag%*******-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB005HNF01O"               }             }             [1]=>             object(AmazonProduct_ItemLink)#17 (1) {               ["_values":protected]=>               array(2) { 

I mean it also has array inside objects. I would like to convert all of them into a multidimensional array.

like image 484
Giri Avatar asked Nov 26 '12 15:11

Giri


People also ask

How to convert object into an array in PHP?

Converting an object to an array with typecasting technique: php class bag { public function __construct( $item1, $item2, $item3){ $this->item1 = $item1; $this->item2 =$item2; $this->item3 = $item3; } } $myBag = new bag("Books", "Ball", "Pens"); echo "Before conversion :".

How to convert multidimensional array to json in PHP?

A combination of PHP's JSON encode and decode functions can magically do the conversion. $obj = json_decode(json_encode($array)); First, the json_encode($array) converts the entire multi-dimensional array to a JSON string. Then the json_decode($string) will convert the JSON string to a stdClass object.

What is a multidimensional object?

A multidimensional array or object is one that has one or more nested arrays or objects as property values.


1 Answers

I know this is old but you could try the following piece of code:

$array = json_decode(json_encode($object), true);

where $object is the response of the API.

like image 104
kendepelchin Avatar answered Oct 04 '22 11:10

kendepelchin