Mansoor H
Mansoor H

Reputation: 604

Remove nested stdClass Object from single stdClass Object

i need to remove nested stdClass Object. Now am get stdClass Object like this,

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
            )
    )
)

But i need stdClass Object like this,

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
            )
    )
 )

any idea to remove nested stdClass Object. am using codeigniter3. Can please help me.

Thanks in advance.

Upvotes: 1

Views: 1601

Answers (1)

Oscar Lidenbrock
Oscar Lidenbrock

Reputation: 816

You can try with this code:

/* 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 */

Upvotes: 2

Related Questions