2

Given the following array:

$array = array(
    'item_1' => array(
            'item_1_1' => array(
                    'item_1_1_1' => 'Hello',
            ),
            'item_1_2' => 'World',
    ),
    'item_2' => array(),
);

How can I convert that into an Object?

Option 1
$obj = (object) $array;

Or

Option 2
$object = json_decode(json_encode($array), FALSE);

Or something else?

I would like to know the difference in the output between the 2 option and understand the best practice for creating this conversion.

andreasonny83
  • 1,213
  • 11
  • 19

2 Answers2

4

Well you are answering somehow your own question, but if you want to have an object with the attributes like your array you have to cast it, this way an array will remain an array

$obj = (object) $array;

OUTPUT:

object(stdClass)#1 (2) {
  ["item_1"]=>
  array(2) {
    ["item_1_1"]=>
    array(1) {
      ["item_1_1_1"]=>
      string(5) "Hello"
    }
    ["item_1_2"]=>
    string(5) "World"
  }
  ["item_2"]=>
  array(0) {
  }
}

if you are using the json_decode version it will convert arrays to objects too:

object(stdClass)#2 (2) {
  ["item_1"]=>
  object(stdClass)#3 (2) {
    ["item_1_1"]=>
    object(stdClass)#4 (1) {
      ["item_1_1_1"]=>
      string(5) "Hello"
    }
    ["item_1_2"]=>
    string(5) "World"
  }
  ["item_2"]=>
  array(0) {
  }
}

NOTE: just the empty array will be an array here.

To Answer your question: The best practice depends on what YOU need.

swidmann
  • 2,787
  • 1
  • 18
  • 32
  • 1
    Posted an answer saying the same basic thing, but added a note about casting objects to arrays breaking numeric indexes in some cases, mainly for completeness' sake – Elias Van Ootegem Dec 07 '15 at 15:50
  • 1
    @EliasVanOotegem, that's a good point, the same thing happens when you simple cast an object to array, for this the function `get_object_vars()` is useful: [Access array element indexed by numerical string](http://stackoverflow.com/q/33696119/5297359) – swidmann Dec 07 '15 at 15:56
1

It depends, really: if you are working on data that might be an array in one case, and an object the next, it would probably be best to use the json_decode trick, simply because unlike a cast, its result is "recursive". There is one very important thing to keep in mind here, though: numeric indexes can, and probably will cause problems for you at some point in time. Take a look at this bug report

This is documented here, but not in a way that really stands out:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible;

Exampe of the problem:

$data = [
    'foo' => 'bar',
    123   => 'all is well',
];
$obj = json_decode(json_encode($data));
var_dump($obj->foo);//bar
var_dump($obj->{123});//all is well
$cast = (array) $obj;
var_dump($cast);//shows both keys
var_dump(isset($cast[123]));//FALSE!!!
var_dump(isset($cast['123']));//FALSE

Basically: If you start converting arrays to objects and back again, numeric keys are not reliable anymore. If I were you, I'd simply change the code that is passing the data where possible, or I'd create a value object that can be set using an array or an object, and normalize the data that way.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149