Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Aug 222010
 

Convert array to object in PHP

Answer:

It is easy to cast an object to an array in PHP, using the following method.

<?php

    $obj = new stdClass(); 
    $obj->foo = "foo";
    $obj->bar = "bar";

    var_dump ( (array) $obj );

It will print out...

array(2) {
  ["foo"]=>
  string(3) "foo"
  ["bar"]=>
  string(3) "bar"
}

 Leave a Reply

(required)

(required)

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>