27

My array is like:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => demo1
        )
    [1] => stdClass Object
        (
            [id] => 2
            [name] => demo2
        )
    [2] => stdClass Object
        (
            [id] => 6
            [name] => otherdemo
        )
)

How can I convert the whole array (including objects) to a pure multi-dimensional array?

3
  • I want to use the array in my foreach loop Commented May 17, 2012 at 7:49
  • 1
    foreach can be used to iterate over an instance of stdClass just like it can be used to iterate over an array. Given your need, no conversion is required. Commented May 17, 2012 at 7:50
  • 1
    why not use the object in your foreach loop ? Commented May 17, 2012 at 7:51

7 Answers 7

44

Have you tried typecasting?

$array = (array) $object;

There is another trick actually

$json  = json_encode($object);
$array = json_decode($json, true);

You can have more info here json_decode in the PHP manual, the second parameter is called assoc:

assoc

When TRUE, returned objects will be converted into associative arrays.

Which is exactly what you're looking for.

You may want to try this, too : Convert Object To Array With PHP (phpro.org)

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

1 Comment

$array = (array) $object; would not work for inner child elements
10

Just use this :

json_decode(json_encode($yourArray), true);

1 Comment

Ibrahim had already provided this technique back in 2012.
7

You can use array_walk to convert every item from object to array:

function convert(&$item , $key)
{
   $item = (array) $item ;
}

array_walk($array, 'convert');

1 Comment

does not work with multidmensional array of objects with array of objects
2

Assuming you want to get to this pure array format:

Array
(
  [1] => "demo1",    
  [2] => "demo2",
  [6] => "otherdemo",
)

Then I would do:

$result = array();
foreach ($array as $object)
{
    $result[$object->id] = $object->name
}

(edit) Actually that's what I was looking for possibly not what the OP was looking for. May be useful to other searchers.

Comments

1

You should cast all objets, something like :

$result = array();
foreach ($array as $object)
{
    $result[] = (array) $object
}

Comments

1

As you are using OOP, the simplest method would be to pull the code to convert itself into an array to the class itself, you then simply call this method and have the returned array populate your original array.

class MyObject {

    private $myVar;
    private $myInt;

    public function getVarsAsArray() {

        // Return the objects variables in any structure you need
        return array($this->myVar,$this->myInt);

    }

    public function getAnonVars() {

        // If you don't know the variables
        return get_object_vars($this);
    }
}

See: http://www.php.net/manual/en/function.get-object-vars.php for info on get_object_vars()

Comments

1

it you have object and you want to set a value as array use

$this->object->pluck('name');

then you get the value as array of names like

["name1", "name2", "name3"];

1 Comment

Why does this answer completely ignore the OP's provided data?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.