Convert Array to Object in PHP

Share

When you need to convert an Array to Object in PHP there is a quick way to do this:

$array = (
'foo' => 'foo',
'bar' => 'bar'
);
$object = (object) $array();?>

This snippet of code covert the array to object, but not recursively. You can get more info on PHP Objects page.

You can easily convert an array to an object recursively if you check the variable type and build the object with the help of variable variables:

function array_to_object($array) {
$object = new stdClass();
foreach($array as $key => $value) {
$object->$key = is_array($value) ? array_to_object($value) : $value;
}
return $object;
}

$array = (
'foo' => 'foo',
'bar' => 'bar'
'arr' => array(
'foo' => 'foo'
'bar' => 'bar'
)
);
$object = array_to_object($array);?>

So if you need to convert the array recursively you need to use the second method, else the first method is more reliable.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

3 thoughts on “Convert Array to Object in PHP”
  • Very Good site, thank yo mister, it’s help’s me!

    June 25, 2010 at 1:38 pm
  • Bingww says:

    This is my first visit here, but I will be back soon, because I really like the way you are writing, it is so simple and honest

    June 29, 2010 at 9:19 pm
  • mutuelles says:

    Thank’s for this nice tips

    September 14, 2010 at 8:16 am

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close