Transform multidimensional array into one-dimensional

4

How to turn this array into PHP:

 array:6 [
      0 => array:1 [
        "EF1A" => "00001"
      ]
      1 => array:1 [
        "EF2A" => "00001"
      ]
      2 => array:1 [
        "EF3A" => "00003"
      ]
      3 => array:1 [
        "EF4A" => "00005"
      ]
      4 => array:1 [
        "EF5A" => "00001"
      ]
      5 => array:1 [
        "EF6A" => "00001"
      ]
      6 => array:1 [
        "EF1A" => "00002"
      ]
    ]

In this?

array:6 [          
        "EF1A" => "00001"         
        "EF2A" => "00001"          
        "EF3A" => "00003"          
        "EF4A" => "00005"          
        "EF5A" => "00001"          
        "EF6A" => "00001"
        "EF1A" => "00002"
    ]
    
asked by anonymous 05.08.2016 / 16:19

2 answers

2

You can do this:

$merged = call_user_func_array('array_merge', $array);

$array being your two-dimensional (original) array

To keep the original keys the same so that we can work them later we will have to adopt a more 'manual' solution and put a prefix eg:

$array = array(
    array('fde432' => 1),
    array('fde1' => 7),
    array('fde3' => 2),
    array('fde1' => 6),
    array('fde3' => 3),
    array('fde2' => 5),
    array('fde1' => 4),
);

$merged = array();
$keys = array();
foreach($array as $key => $arr) {
    $innerKey = array_keys($arr)[0];
    if(!isset($keys[$innerKey])) {
        $keys[$innerKey] = 0;
    }
    else {
        $keys[$innerKey] += 1;
    }
    $prefix = $keys[$innerKey];
    $merged[$prefix. '_' .$innerKey] = $arr[$innerKey];
}
echo '<pre>', print_r($merged), '</pre>';

Output:

(
    [0_fde432] => 1
    [0_fde1] => 7
    [0_fde3] => 2
    [1_fde1] => 6
    [1_fde3] => 3
    [0_fde2] => 5
    [2_fde1] => 4
)
    
05.08.2016 / 16:21
3

I think there is a simpler way, that would be using the array_walk_recursive function. Considering that the indexes of sub-array are not identical, we can use it to create a array one-dimensional.

See:

$flatted = [];

array_walk_recursive($array, function ($value, $key) use(&$flatted) {   
    $flatted[$key] = $value;     
});

The array_walk_recursive function traverses all values of array recursively, through a callback. It passes the first argument to function as the value, and the second argument to the key.

We use the keyword use to use an external variable within the scope of our anonymous function. By adding the & operator we are doing an assignment by reference; that is, changes made within the closure will be made in the original variable $flatted .

Another addition I would like to make is if you use array_merge , as mentioned in the other question, if you are using PHP 5.6> , you can do this:

 array_merge(... $array)

This simplifies the process by avoiding the call of call_user_func_array .

The ellipsis sign is named ellipses in some cases. These are the varied functions . You can read more about this subject here:

What is the name of the operator ... used in PHP 5.6?

What can change with the implementation of variadic function?

Examples on Ideone

    
05.08.2016 / 17:50