如何组合两个不同的多维数组(PHP)

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"
        ]
    ]
];
douzhao6584
douzhao6584 他们应该匹配他们的位置。$data的第一个元素应该与没有$structure元素的第一个键组合。
大约一年之前 回复
doulangpeng3933
doulangpeng3933 标准是什么?是应该按顺序设置值还是低位键与上限值匹配?
大约一年之前 回复

4个回答

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

dongle7637
dongle7637 这对我有用。 谢谢。
大约一年之前 回复

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.

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;
dongliang_bj2016
dongliang_bj2016 你在哪里用过$ data?
大约一年之前 回复



使用
$ array = array_merge($ structure,$ data);
更多信息请点击此链接
如何在PHP中加入两个多维数组 </ p>
</ DIV>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问