• Convert PHP stdClass to Array and Array to stdClass

    Every PHP coders have come across Arrays and stdClass Objects. Sometimes it’s very useful convert Objects to Arrays and Arrays to Objects. This is easy if arrays and objects are one-dimensional, but might be little tricky if using multidimensional arrays and objects.This post defines two ultra simple recursive function to convert multidimensional Objects to Arrays and multidimensional Arrays to Objects.

    Convert stdClass Objects to Multidimensional Arrays

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
     
    <?php 
     
                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;
     
                            }
     
                }
     
    ?>

    Convert Multidimensional Arrays to stdClass Objects

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
     
    <?php
     
                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;
     
                            }
     
                }
     
    ?>

    Function usage

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
     
    <?php
     
    // Create new stdClass Object
     
                $init = new stdClass;
     
     
     
                // Add some test data
     
                $init->foo = "Test data";
     
                $init->bar = new stdClass;
     
                $init->bar->baaz = "Testing";
     
                $init->bar->fooz = new stdClass;
     
                $init->bar->fooz->baz = "Testing again";
     
                $init->foox = "Just test";
     
     
     
                // Convert array to object and then object back to array
     
                $array = objectToArray($init);
     
                $object = arrayToObject($array);
     
     
     
                // Print objects and array
     
                print_r($init);
     
                echo "\n";
     
                print_r($array);
     
                echo "\n";
     
                print_r($object);
     
    ?>

    Using above code we can convert object to array and array to object format which can be easy to maintain programming.

    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    3 − = zero