Shreyas Achar
Shreyas Achar

Reputation: 1435

Convert Multidimensional array to single array in codeigniter php

I have a multidimensional array as shown below

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 237
                )
            [1] => stdClass Object
                (
                 [id] => 228
                )
        )
    [1] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 247
                )
            [1] => stdClass Object
                (
                 [id] => 238
                )
        )
)

I want to convert into single array as shown below

Array
(
        [0] => stdClass Object
        (
            [id] => 237
        )
        [1] => stdClass Object
        (
            [id] => 228
        )
        [2] => stdClass Object
        (
            [id] => 247
        )
        [3] => stdClass Object
        (
            [id] => 238
        )
)

I have tried with the following solution Convert multidimensional array into single array

But the result am not getting Its Coming Null

How to get the desired result for the above input.

Any help appreciated.

Upvotes: 0

Views: 1163

Answers (2)

NiroshanJ
NiroshanJ

Reputation: 578

Hope this one works

function array_flattern($arr) {
    $returnArr=[];
    foreach($arr as $k=>$v) {
        $returnArr = array_merge($returnArr, $v);
    }
    return $returnArr;
}

Upvotes: 0

Luffy
Luffy

Reputation: 1026

Try foreach loop then array_merge()

$result = [];

foreach ($array as $value) {
    $result = array_merge($result, $value);
}

var_dump($result);

Upvotes: 4

Related Questions