Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert multidimensional array to object in php?

Tags:

i have a multidimensional array:

$image_path = array('sm'=>$sm,'lg'=>$lg,'secondary'=>$sec_image); 

witch looks like this:

[_media_path:protected] => Array             (                 [main_thumb] => http://example.com/e4150.jpg                 [main_large] => http://example.com/e4150.jpg                 [secondary] => Array                     (                         [0] => http://example.com/e4150.jpg                         [1] => http://example.com/e4150.jpg                         [2] => http://example.com/e9243.jpg                         [3] => http://example.com/e9244.jpg                     )              ) 

and i would like to convert it into an object and retain the key names.

Any ideas?

Thanks

edit: $obj = (object)$image_path; doesn't seem to work. i need a different way of looping through the array and creating a object

like image 479
Patrioticcow Avatar asked Feb 07 '12 01:02

Patrioticcow


People also ask

How do you object in PHP?

To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.

Is multidimensional array PHP?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

What is the use of multidimensional array in PHP?

PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.


2 Answers

A quick way to do this is:

$obj = json_decode(json_encode($array)); 

Explanation

json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)

json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)

I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.

like image 84
Charlie Schliesser Avatar answered Oct 24 '22 20:10

Charlie Schliesser


The best way would be to manage your data structure as an object from the start if you have the ability:

$a = (object) array( ... ); $a->prop = $value; //and so on 

But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).

You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:

function convert_array_to_obj_recursive($a) {     if (is_array($a) ) {         foreach($a as $k => $v) {             if (is_integer($k)) {                 // only need this if you want to keep the array indexes separate                 // from the object notation: eg. $o->{1}                 $a['index'][$k] = convert_array_to_obj_recursive($v);             }             else {                 $a[$k] = convert_array_to_obj_recursive($v);             }         }          return (object) $a;     }      // else maintain the type of $a     return $a;  } 

Hope that helps.

EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. $o->1 or $o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.

like image 42
Wayne Weibel Avatar answered Oct 24 '22 19:10

Wayne Weibel