将对象转换为数组

时间:2012-12-20 00:22:54

标签: php foreach

  

可能重复:
  Converting array and objects in array to pure 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
                )

        )

到此:

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
        )

)

我坚持使用foreach循环来执行此操作。

修改

Here is a screenshot of the output

2 个答案:

答案 0 :(得分:3)

我会以懒惰的方式做到这一点:

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

Php Doc

  

<强> ASSOC
  如果为TRUE,则返回的对象将转换为关联数组。

编辑:我刚测试过这个:

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

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

这是输出:

array(2) {
  [0]=>
  array(1) {
    ["property"]=>
    string(8) "somepath"
  }
  [1]=>
  array(1) {
    ["property"]=>
    string(8) "somepath"
  }
}

答案 1 :(得分: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;
    }
}

不完全确定它会起作用。可能更容易做出懒惰的json_decode(json_encode($obj),true)方式,就像@Ibu说的那样。