PHP stdClass to Array and Array to stdClass Object

PHP stdClass to Array and Array to stdClass Object

I think every PHP coders have come accross Arrays and stdClass Objects (belongs to PHP Predefined Classes). 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.

Function to Convert stdClass Objects to Multidimensional Arrays

<?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;
		}
	}

?>

Function to Convert Multidimensional Arrays to stdClass Objects

<?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

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

Test Output :

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
)

Related Posts


5 thoughts on “PHP stdClass to Array and Array to stdClass Object

  1. win money playing poker

    I have been exploring for a little bit for any high-quality articles or blog posts on this sort of space . Exploring in Yahoo I finally stumbled upon this web site. Reading this information So i am satisfied to convey that I have an incredibly just right uncanny feeling I came upon exactly what I needed. I so much no doubt will make certain to do not forget this web site and provides it a glance on a constant basis.

    Reply
  2. kore dizileri izle

    Nice a person webpage operator accomplishment blog article excellent sharings in such a blog site always have fun

    Reply
  3. Magnolia

    Good put up admin! i bookmarked your word wide web web site. i will look ahead in case you will have an e-mail checklist including.

    Reply
  4. private krankenversicherung vergleich stiftung warentest

    Thank you, I have just been searching for information approximately this subject for ages and yours is the best I have discovered till now. But, what in regards to the conclusion? Are you positive concerning the supply?|What i do not realize is actually how you’re now not actually a lot more smartly-liked than you may be now. You’re so intelligent.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>