0

I want to combine two different multi-dimensional arrays, with one providing the correct structure (keys) and the other one the data to fill it (values).

Notice that I can't control how the arrays are formed, the structure might vary in different situations.

$structure = [
    "a",
    "b" => [
        "b1",
        "b2" => [
            "b21",
            "b22"
        ]
    ]
];

$data = [A, B1, B21, B22];

Expected result:

$array = [
    "a" => "A",
    "b" => [
        "b1" => "B1",
        "b2" => [
            "b21" => "B21",
            "b22" => "B22"
        ]
    ]
];
Nuno Peixoto
  • 144
  • 1
  • 10
  • What are the criteria? Should the values be set in order or should the lower-cased keys match upper-cased values? – showdev Jul 24 '19 at 12:05
  • They should match by their place. The first element of $data should combine with the first key without element of $structure. – Nuno Peixoto Jul 25 '19 at 08:18

4 Answers4

1

You can use the following code, however it will only work if number of elements in $data is same or more than $structure.

$filled = 0;
array_walk_recursive ($structure, function (&$val)  use (&$filled, $data) {
    $val = array( $val => $data[ $filled ] ); 
    $filled++; 
});

print_r( $structure );

Here is a working demo

ascsoftw
  • 3,466
  • 2
  • 15
  • 23
1

You can try by a recursive way. Write a recursive method which takes an array as first argument to alter and the data set as its second argument. This method itself call when any array element is another array, else it alters the key and value with the help of data set.

$structure = [
    "a",
    "b" => [
        "b1",
        "b2" => [
            "b21",
            "b22"
        ]
    ]
];

$data = ['A', 'B1', 'B21', 'B22'];


function alterKey(&$arr, $data) {
    foreach ($arr as $key => $val) {
        if (!is_array($val)) {
            $data_key = array_search(strtoupper($val), $data);
            $arr[$val] = $data[$data_key];
            unset($arr[$key]);
        } else {
            $arr[$key] = alterKey($val, $data);
        }
    }

    ksort($arr);
    return $arr;
}

alterKey($structure, $data);

echo '<pre>', print_r($structure);

Working demo.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

This should work.

   $structure = [
    "a",
    "b" => [
    "b1",
    "b2" => [
        "b21",
        "b22"
            ]
      ]
];

$new_structure = array();
foreach($structure as $key =>$value)
{
  if(!is_array($value))
    {
      $new_structure[$value]= $value;
    }else{
      foreach($value as $k =>$v)
        {
          if(!is_array($v))
           { 
              $new_structure[$key][$v]=$v; 
           }else
           {
             foreach($v as $kk => $vv)
             {
                   $new_structure[$k][$vv]=$vv;                                   
             }            
           }
        }
     }
}
print_r($new_structure);exit;
Red Bottle
  • 2,839
  • 4
  • 22
  • 59
-1

Use $array=array_merge($structure,$data); for more information follow this link how to join two multidimensional arrays in php