PHP Array to Object and Object to Array

Circular ArrowIn your php applications you may find many circumstances where it is needed to convert an object into an array and an array into an object.

One common example of this usage is in Server <-> Client communications.  The server may want to store items in an array for sorting and iteration.  By contrast, the client may want data as an object if the client language cannot interact with associative arrays.

The purpose of this post is not to get into the debate of when to use an array and when to use an object.  Rather, it’s purpose is to share handy functions for translating data from object for into array form and vice-versa.  From array form into object form.

Converting an Object to an Array

Before we share the code, someone may ask, “Doesn’t PHP have built in functions for this”?  The answer is yes.  There are some built in functions.  However I have found that my code runs smoother when i wrap some of the built in functionality, with a little bit of application logic to make it handle more convenient.

For example.  If we had an object and wanted to convert it to an array.  There are several options available to us.

First is that we could simply typecast it into an array:

$array = (array)$object;

Another option is that we can actually ENCODE it in JSON and then DECODE it in the same line.  When we DECODE is from JSON we provide a flag to specify that we want it decoded into an associative array.

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

I personally favor the first option, however I like to add a little more application logic to the routine.  Here is my objectToArray function

 * Helper utility to convert an object into an array
 * @params object $object The object to be converted to an array
 * @params mixed $errorValue The default value to use on an error in the type casting
 * @returns array|mixed Returns either 1) the data from the object in an array or 2) and empty array on error of 2) the passed error value as specified in the params
 public static function objectToArray($object, $errorValue=array()){
     return (array)$object;
   }catch(Exception $e){
     error_log("Error caught while trying to convert an object to array.");
     return $errorValue;

In this function I use the type cast approach.  However I have the call wrapped around a try catch so that I can handle errors.  I then also provide an option for handling that error.  I can either return an empty array by default or I can allow the calling function to specify what the erro value should be, such as a null or false.

Converting an Array to an Object

Now let’s look at the reverse, converting an array to an object.  Like before there are some simple routines in PHP that can be used.

First is that we can repurpose the JSON example above.  We can JSON ENCODE the array, then JSON DECODE the data, making sure to pass a false into the routine so that it is decoded as an object

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

Another approach is to create a new Standard Object class and iterate through the array elements setting them into the object one by one.

$object = new stdClass();
foreach($array as $k=>$v){
  $object->$k = $v;

While the JSON approach is clean and easy, I personally prefer the iteration approach.  However I don’t ever use it in the format above.  Rather I wrap it into my own function and add some additional controls.  For example, what do you do when one of the elements is an array?  Do you convert it to an object as well?  My approach below adds some additional options to make it more universal and customizable.

 * Converts an array to an object
 * @params Array $array the array to be converted to an object
 * @params bool $recursive Whether or not to convert child arrays to child objects
 * @returns Object $object
public static function arrayToObject($array, $recursive=true) {
  $object = new stdClass;
  foreach($array as $k => $v) {
    if($recursive && is_array($v)) {
      $object->{$k} = self::arrayToObject($v, $recursive);
    } else {
      $object->{$k} = $v;
  return $obj;

Well – I hope these are helpful.  let me know what you think of them below.



Posted in , and tagged , .