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.

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"
}
  1. Clone an object in PHP
  2. How to remove object from array inside foreach loop?
  3. How to convert hash to array in Perl?
  4. A simple array in Perl
  5. Array reference in Perl

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>