1

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.

7
  • 1
    You should add your model where it comes from. Commented Jul 14, 2015 at 7:40
  • its comes from model. But i need to remove this nested stdClass Object in the model only. Commented Jul 14, 2015 at 7:43
  • ^ it is better to generate correct array Commented Jul 14, 2015 at 7:43
  • You have too loop and create new array Commented Jul 14, 2015 at 7:46
  • How ? any idea to create new array Commented Jul 14, 2015 at 7:48

1 Answer 1

2

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 */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.