2

可能重复:
将数组和数组中的对象转换为纯数组

我现在有一个数组,但它被传递给另一个将其转换为对象的函数,尽管它需要是一个标准数组,但它才能工作。我需要将以下对象数组转换为标准数组:

[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 循环上来做到这一点。

编辑:

这是输出的屏幕截图

4

2 回答 2

3

我会用懒惰的方式来做:

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

php文档

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"
  }
}
于 2012-12-20T00:25:35.930 回答
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 说的那样,用懒惰的方式可能更容易。

于 2012-12-20T00:26:04.683 回答