Transforming Multidimensional Array into One-Dimensional

Asked

Viewed 1,476 times

5

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"
    ]

2 answers

4

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

Behold:

$flatted = [];

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

The function array_walk_recursive runs through all the values of array recursively, through a callback. It passes the first argument to function as the value, and the second, 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 making an assignment by reference; i.e., changes made within the closure will be made to the original variable $flatted.

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

 array_merge(... $array)

This simplifies the process by avoiding the call from call_user_func_array.

The reticence sign is named ellipses in some cases. These are variable functions. You can read more about it here:

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

What can change with the variadic Function implementation?

Examples in Ideone

  • 1

    Do you believe there is or does exist?

3


You can do it like this:

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

$array its two-dimensional array (original)

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

$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
)
  • This way, keys of q arrays are equal are removed. I need them to be rs

  • 3

    This is impossible, you can’t have equal keys in the same layer of the @Amandalima array. Now do: array('ola' => 1, 'ola' => 3); only one of them remains (the last)

  • I get it. Thank you

  • @Amandalima however I will put a solution to this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.