XerXeX
XerXeX

Reputation: 792

How to shorten a multi dimensional array in php

I have a array that looks like this

Array
(
    [11] => Array
        (
            [1] => Array
                (
                    [type] => OPT 
                    [panel] => 1
                    [loop] => 1
                    [number] => 1
                    [zone] =>  1  
                    [value] => 38  
                )

        )

    [12] => Array
        (
            [1] => Array
                (
                    [type] => OPT 
                    [panel] => 1
                    [loop] => 2
                    [number] => 1
                    [zone] => 19  
                    [value] => 40  
                )

        )

)

I want to delete the first dimension so that it looks like this

Array
(
    [0] => Array
         (
                [type] => OPT 
                [panel] => 1
                [loop] => 1
                [number] => 1
                [zone] =>  1  
                [value] => 38  
         )

    [1] => Array
        (
                [type] => OPT 
                [panel] => 1
                [loop] => 2
                [number] => 1
                [zone] => 19  
                [value] => 40  
        )

)    

How do I do that? Sorry but I have to put in some test or the compiler won't let me post the code.

Upvotes: 1

Views: 109

Answers (3)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

You can simply use call_user_func_array as

$result = call_user_func_array('array_merge',$your_array);

Demo

Upvotes: 3

Sumeet
Sumeet

Reputation: 1799

$short = array();

foreach($long as $k=>$v) {
  $short[] = $array[$k][0];
}

var_dump($short);

provided that you have only one element in 2nd level

Upvotes: 1

Rizier123
Rizier123

Reputation: 59701

This should work for you:

Just go through each sub array and array_merge() the arrays with your $result together, e.g.

<?php

    $result = [];

    foreach($arr as $v)
        $result = array_merge($result, $v);

    print_r($result);

?>

Demo

Upvotes: 0

Related Questions