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

Community
  • 1
  • 1
Mick Davies
  • 93
  • 1
  • 2
  • 11
  • Starting with [this answer](http://stackoverflow.com/questions/10631767/converting-array-and-objects-in-array-to-pure-array) cast the resultant object of arrays to an array with `(array)$object` – Michael Berkowski Dec 20 '12 at 00:25

2 Answers2

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"
  }
}
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • +1, I do like lazy things :-) – Alain Tiemblo Dec 20 '12 at 00:28
  • 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); – Mick Davies Dec 20 '12 at 00:37
  • the first array listed above is produced again using the json method $array = json_decode($jsonstring,true); – Mick Davies Dec 20 '12 at 00:44
  • Brilliant answer, made me laugh, so easy :-) – Green Black Dec 20 '12 at 00:44
  • @MickDavies is the array resulting not what you are looking for? – Ibu Dec 20 '12 at 00:49
  • It's not right, I'm printing it out as another variable so I can tell it's new but using the code above, produces exactly the same print out as the first code box I've listed. It's still got stdClass Object in the same places etc... – Mick Davies Dec 20 '12 at 00:51
  • @MickDavies See my Edit, i just tested it, and it seems to work – Ibu Dec 20 '12 at 00:57
  • yeah i checked your example and it put it back as a stdClass so I'm thinking it's the response converting everything to objects, so I can't get an accurate test of what is being sent. This is using Phil Sturgeons CI Rest Server... struggling bad with uploading multiple files over it's POST API – Mick Davies Dec 20 '12 at 01:06
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.

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • 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](http://codepad.org/HrlzBV69). – Alain Tiemblo Dec 20 '12 at 00:37
  • Very true but I was just going for how to change the above object into the above array. – qooplmao Dec 20 '12 at 00:41
  • :) of course, this does not make your answer worser. – Alain Tiemblo Dec 20 '12 at 00:41
  • 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. – qooplmao Dec 20 '12 at 00:43