0

How can i remove the first dimansion of a multidemensional Array without loosing the keys? i have an Array that have multiple arrays inside the firstkey is a Date and the secondkey is the hour.

my Output is:

Array
(
    [0] => Array
        (
            [firstkey] => Array
                (
                    [secondkey] => Array
                    (
                        [0] => 7
                        [1] => 8
                    )
                )
            )
    [1] => Array
        (
            [firstkey] => Array
                (
                    [secondkey] => Array
                    (
                        [0] => 7
                        [1] => 8
                    )
                )
        )
)   

and i want this:

Array
(
   [firstkey] => Array
               (
                   [secondkey] => Array
                   (
                       [0] => x
                       [1] => y
                       [2] => z
                       [3] => r
                   )
               )
  
)

i also tried array_merge_recursive() but instead of putting the values to the secondkey it creates a new array with an incremental key

LeGerman
  • 23
  • 1
  • 5

1 Answers1

0

okay found a solution on: PHP : multidimensional array merge recursive

function array_merge_recursive_ex(array $array1, array $array2)
{
    $merged = $array1;

    foreach ($array2 as $key => & $value) {
        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = array_merge_recursive_ex($merged[$key], $value);
        } else if (is_numeric($key)) {
             if (!in_array($value, $merged)) {
                $merged[] = $value;
             }
        } else {
            $merged[$key] = $value;
        }
    }

    return $merged;
}

and with this function i can merge that mutlidemensional array without loosing keys:

$newarray =[];
foreach($array as $firstkey){
    $newarray = array_merge_recursive_ex($newarray, $firstkey);
}
LeGerman
  • 23
  • 1
  • 5