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.

3
  • 1
    the attempt with json seems fine to me Commented Dec 7, 2015 at 15:11
  • 3
    Duplicate? stackoverflow.com/questions/1869091/… Commented Dec 7, 2015 at 15:14
  • my answer is don't... unless you have a real good reason. arrays are more flexible and usable than objects. just leave it alone. Commented Dec 7, 2015 at 15:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.