从单个stdClass对象中删除嵌套的stdClass对象

时间:2015-07-14 07:37:30

标签: php arrays codeigniter

我需要删除嵌套的stdClass对象。现在我得到像这样的stdClass对象,

Array
(
[0] => Array
    (
        [0] => stdClass Object
            (
                [cs_id] => 1
                [cs_service_name] => 2
            )
    )

[1] => Array
    (
        [0] => stdClass Object
            (
                [cs_id] => 2
                [cs_service_name] => 3
            )

        [1] => stdClass Object
            (
                [cs_id] => 6
                [cs_service_name] => 3
            )
    )

[2] => Array
    (
        [0] => stdClass Object
            (
                [cs_id] => 7
                [cs_service_name] => 4
            )
    )
)

但我需要像这样的stdClass对象,

Array
(
[0] => Array
    (
        [0] => stdClass Object
            (
                [cs_id] => 1
                [cs_service_name] => 2
            )
        [1] => stdClass Object
            (
                [cs_id] => 2
                [cs_service_name] => 3
            )
        [2] => stdClass Object
            (
                [cs_id] => 6
                [cs_service_name] => 3
            )
        [3] => stdClass Object
            (
                [cs_id] => 7
                [cs_service_name] => 4
            )
    )
 )

删除嵌套的stdClass对象的任何想法。我正在使用codeigniter3。可以帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以尝试使用以下代码:

/* you have a $collection array with all objects */

$newCollection = array();
foreach ($collection as $item) {
    if (is_array($item) && count($item)) {
        foreach ($item as $subItem) {
            $newCollection[] = $subItem;
        }
    }
}

/* $newCollection is the new array collection */