2

Possible Duplicate:
Converting array and objects in array to pure array

I have an array at the moment but it is being passed to another function which is converting it to objects, for it to work though it needs to be a standard array. I need to convert the following object array to a standard array:

[files] => stdClass Object
        (
            [1] => stdClass Object
                (
                    [name] => price-my-insurance.jpg
                    [type] => image/jpeg
                    [tmp_name] => /Applications/MAMP/tmp/php/phpfmRfyN
                    [error] => 0
                    [size] => 911376
                )

            [2] => stdClass Object
                (
                    [name] => sideshows.jpg
                    [type] => image/jpeg
                    [tmp_name] => /Applications/MAMP/tmp/php/phpTamdHy
                    [error] => 0
                    [size] => 967656
                )

            [3] => stdClass Object
                (
                    [name] => the-beer-scale.jpg
                    [type] => image/jpeg
                    [tmp_name] => /Applications/MAMP/tmp/php/phpwCmwlW
                    [error] => 0
                    [size] => 742219
                )

            [4] => stdClass Object
                (
                    [name] => the-little-lace.jpg
                    [type] => image/jpeg
                    [tmp_name] => /Applications/MAMP/tmp/php/phpFnUuf8
                    [error] => 0
                    [size] => 939963
                )

            [5] => stdClass Object
                (
                    [name] => varrstoen-australia.jpg
                    [type] => image/jpeg
                    [tmp_name] => /Applications/MAMP/tmp/php/phpUtWyk1
                    [error] => 0
                    [size] => 2204400
                )

        )

to this:

Array
(
    [1] => Array
        (
            [name] => price-my-insurance.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpfmRfyN
            [error] => 0
            [size] => 911376
        )

    [2] => Array
        (
            [name] => sideshows.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpTamdHy
            [error] => 0
            [size] => 967656
        )

    [3] => Array
        (
            [name] => the-beer-scale.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpwCmwlW
            [error] => 0
            [size] => 742219
        )

    [4] => Array
        (
            [name] => the-little-lace.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpFnUuf8
            [error] => 0
            [size] => 939963
        )

    [5] => Array
        (
            [name] => varrstoen-australia.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpUtWyk1
            [error] => 0
            [size] => 2204400
        )

)

I'm stuck on the foreach loop to do this.

EDIT:

Here is a screenshot of the output

1
  • Starting with this answer cast the resultant object of arrays to an array with (array)$object Commented Dec 20, 2012 at 0:25

2 Answers 2

3

I would do it the lazy way:

$jsonstring = json_encode($theObj);
$array = json_decode($jsonstring,true);

Php Doc

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

Edit: I just tested this:

<?php
$o = new stdClass();
$o->property = "somepath";

$a = array($o,$o);
$js = json_encode($a);
$array = json_decode($js,true);
var_dump($array);
?>

and here is the output:

array(2) {
  [0]=>
  array(1) {
    ["property"]=>
    string(8) "somepath"
  }
  [1]=>
  array(1) {
    ["property"]=>
    string(8) "somepath"
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

+1, I do like lazy things :-)
I can't get it to work, or it's not doing anything... $files = $this->post('userfile'); $jsonstring = json_encode($files); $array = json_decode($jsonstring,true);
the first array listed above is produced again using the json method $array = json_decode($jsonstring,true);
Brilliant answer, made me laugh, so easy :-)
@MickDavies is the array resulting not what you are looking for?
|
0
$array = array();

foreach ($files as $k => $v) {
    if (!is_array($array[$k])) {
        $array[$k] = array();
    }

    foreach ($v as $j => $u) {
        $array[$k][$j] = $u;
    }
}

Not entirely sure it will work. Probably easier to do the lazy json_decode(json_encode($obj),true) way, like @Ibu says.

4 Comments

I think you'll get some troubles using such a way, because of types conversions that affects arrays but not object properties. Have a look to this codepad.
Very true but I was just going for how to change the above object into the above array.
:) of course, this does not make your answer worser.
Also if you remove the $array = (array)$obj; and change it to foreach ($obj then it seems to work again. I only know minimal stuff about StdClass objects (or none standard objects, if you know what I mean) so it's all guesswork for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.