Home  >  Article  >  Backend Development  >  Conversion between PHP objects and arrays

Conversion between PHP objects and arrays

巴扎黑
巴扎黑Original
2016-11-23 13:35:10791browse

/**

* Conversion between PHP objects and arrays

*

* @author flyer0126

* @since 2012/05/03

**/


// 1. Use (array) and (object) to simply process

$objTemp = (object)array();

$objTemp->a = 1;

$objTemp->b = 2;

$objTemp->c = 3;

$arrTemp = (array)$objTemp;

print_r($objTemp);

print_r($arrTemp) ;


 /**

stdClass Object

(

    [a] => 1

    [b] => 2

    [c] => 3

)

Array

(

    [a] => 1

    [b] => 2

    [c] => 3

)

**/


// PS: Simple (array) and (object) can only handle single-layer data, for multi-layer array and object conversion There is nothing you can do.


// 2. Conversion processing between multi-dimensional arrays and objects


/**

* Convert object to multi-dimensional array

*

**/

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 array to object

*

**/

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;

}

}


// Useage:

$init = new stdClass;

$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);

print_r($array);

print_r($object) ;


 /**

stdClass Object

(

    [foo] => Test data

    [bar] => stdClass Object

        (

            [baaz] => Testing

            [fooz] => stdClass Object

                (

                    [baz] => Testing again

                )

        )

    [foox] => Just test

)

Array

(

    [foo] => Test data

    [bar] => Array

        (

            [baaz] => Testing

            [fooz] => Array

                (

                    [baz] => Testing again

                )

        )

    [foox] => Just test

)

stdClass Object

(

    [foo] => Test data

    [bar] => stdClass Object

        (

            [baaz] => Testing

            [fooz] => stdClass Object

                (

                    [baz] => Testing again

                )

        )

    [foox] => Just test

)

**/


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Line break problem in phpNext article:Line break problem in php