Monday 22 September 2014

How to convert Objects to Arrays or Arrays to Objects in multi-dimensional arrays and objects ?

Dealing with the consumption de SOAP webservices, I have been encountered some trouble when extracting data with a mix of multidimensional arrays and objects ; a way to simplefy this extraction was to transform objects into array or arrays into objects using a recursive method:

Here below, one will find two recursive functions to convert multi-dimensional Objects To Array and Multidimensional Aarrays to Objects :

Function to Convert stdClass Objects to Multidimensional Arrays:

    function objectToArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }
 
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }

Function to Convert Multidimensional Arrays to stdClass Objects:

    function arrayToObject($d) {
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return (object) array_map(__FUNCTION__, $d);
        }
        else {
            // Return object
            return $d;
        }
    }

Example:
<?php
      // Create new stdClass Object
      $init = new stdClass;
      $init->debut = "14:00";
      $init->fin = "18:00";

      $init2 = array();
      $init2[0]= new stdClass;
      $init2[0]->debut = "09:00";
      $init2[0]->fin = "12:00";
      $init2[1]= new stdClass;
      $init2[1]->debut = "14:00";
      $init2[1]->fin = "19:00";

      var_dump($init);
      var_dump($init2);
     // Convert objects to array and then array to  backobject
      $array = $this->objectToArray($init);
      $array2 = $this->objectToArray($init2);
      var_dump($array);
      var_dump($array2);
      $object = $this->arrayToObject($array);
      $object2 = $this->arrayToObject($array2);
var_dump($array2); function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return object converted to array
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }
    
    
    function arrayToObject($d) {
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return (object) array_map(__FUNCTION__, $d);
        }
        else {
            // Return object
            return $d;
        }
    }
?>

cf. http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/


Using array_map within a class function is not straight forward : the reference to the function has to be written like this : array_map(array($this,__FUNCTION__), $d);

Here is an example of this usage :

<?php

    $Chrono = new Relais();
    $Chrono->convert();

    class Relais {

        public function convert()
        {
            // Create new stdClass Object
            $init = new stdClass;
            $init->debut = "14:00";
            $init->fin = "18:00";

            $init2 = array();
            $init2[0]= new stdClass;
            $init2[0]->debut = "09:00";
            $init2[0]->fin = "12:00";
            $init2[1]= new stdClass;
            $init2[1]->debut = "14:00";
            $init2[1]->fin = "19:00";

           // var_dump($init);
            //var_dump($init2);
            // Convert array to object and then object back to array
            $array = $this->objectToArray($init);
            $array2 = $this->objectToArray($init2);
            var_dump($array);
            var_dump($array2);
            $object = $this->arrayToObject($array);
            $object2 = $this->arrayToObject($array2);
           // var_dump($object);
            var_dump($object2);
        }
        private function objectToArray($d) {
            if (is_object($d)) {
                // Gets the properties of the given object
                // with get_object_vars function
                $d = get_object_vars($d);
            }

            if (is_array($d)) {
                /*
                * Return object converted to array 
                * Using __FUNCTION__ (Magic constant)
                * for recursive call
                */
                return array_map(array($this,__FUNCTION__), $d);
            }
            else {
                // Return array
                return $d;
            }
        }             

        private function arrayToObject($d) {
            if (is_array($d)) {
                /*
                * Return array converted to object
                * Using __FUNCTION__ (Magic constant)
                * for recursive call
                */
                return (object) array_map(array($this,__FUNCTION__), $d);
            }
            else {
                // Return object
                return $d;
            }
        }
    }
    

?>