如何将一个数组中的子数组同步嵌套到另一个数组的子数组中?

时间:2017-11-10 17:09:52

标签: php arrays multidimensional-array pass-by-reference array-map

我有2个多维数组:

$category = array (
    37 = array (id=1, name=joe, boss=kev)
    73 = array (id=55, name=diane, boss=rox)
    11 = array (id=4, name=bideo, boss=julia)
)  

$other_variable = array (
    1 = array (
        picture1 = array (name=zee, id=4),
        picture2 = array (name=izzy, id=1)
    )
    2 = array (
        picture1 = array (name=foo, id=55),
        picture2 = array (name=ido, id=44)        
    )
    3 = array (
        picture1 = array (name=wheez, id=87),
        picture2 = array (name=ardu, id=9)
    )
)  

我想将它们组合起来以便

$category = array (
    37 = array (
        id=1, 
        name=joe, 
        boss=kev, 
        other_variable = array (
            picture1 = array (name=zee, id=4),
            picture2 = array (name=izzy, id=1)
    ),

    73 = array (
        id=55, 
        name=diane, 
        boss=rox, 
        other_variable = array (
            picture1 = array (name=foo, id=55),
            picture2 = array (name=ido, id=44)
    ),
    11 = array (
        id=4, 
        name=bideo, 
        boss=julia, 
        other_variable = array (
            picture1 = array (name=wheez, id=87),
            picture2 = array (name=ardu, id=9)
    )
)  

我试过了

 $new_array = array_map(null, $category, $other_variable);  

它组合了两个数组,但它在数组中创建了几个嵌套级别。我正在寻找一些更清洁的东西,将$category作为父数组。

2 个答案:

答案 0 :(得分:1)

你期待这样的事吗?这里我们使用nextcurrent函数来递增数组的内部指针并获取当前值。

Try this code snippet here

foreach($category as &$value)
{
    $value["other_variable"]=current($other_variable);
    next($other_variable);
}
print_r($category);

答案 1 :(得分:1)

使用array_map()来同步迭代两个数组,你走在正确的道路上,但遗憾的是,这个函数会杀死你的第一级密钥。

来自array_map() manual

  

当且仅当传递了一个数组时,返回的数组将保留数组参数的键。如果传递了多个数组,则返回的数组将具有顺序整数键。

对于修改$category数组的任务,

array_walk()将是一个更清晰/更合适的函数调用。 array_walk()允许您修改$category数组并同步迭代$other_variable数组(作为可选的userdata参数),而无需任何其他迭代函数调用(例如next() )。

代码:(Demo

$category=[
    37=>["id"=>1,"name"=>"joe","boss"=>"kev"],
    73=>["id"=>55,"name"=>"diane","boss"=>"rox"],
    11=>["id"=>4,"name"=>"bideo","boss"=>"julia"]
];

$other_variable=[
    1=>["picture1"=>["name"=>"zee","id"=>4],"picture2"=>["name"=>"izzy","id"=>1]],
    2=>["picture1"=>["name"=>"foo","id"=>55],"picture2"=>["name"=>"ido","id"=>44]],
    3=>["picture1"=>["name"=>"wheez","id"=>87],"picture2"=>["name"=>"ardu","id"=>9]]
];

array_walk($category,function(&$v,$k,$ov){$v['other_variable']=current($ov);},$other_variable);
var_export($category);

输出:

array (
  37 => 
  array (
    'id' => 1,
    'name' => 'joe',
    'boss' => 'kev',
    'other_variable' => 
    array (
      'picture1' => 
      array (
        'name' => 'zee',
        'id' => 4,
      ),
      'picture2' => 
      array (
        'name' => 'izzy',
        'id' => 1,
      ),
    ),
  ),
  73 => 
  array (
    'id' => 55,
    'name' => 'diane',
    'boss' => 'rox',
    'other_variable' => 
    array (
      'picture1' => 
      array (
        'name' => 'zee',
        'id' => 4,
      ),
      'picture2' => 
      array (
        'name' => 'izzy',
        'id' => 1,
      ),
    ),
  ),
  11 => 
  array (
    'id' => 4,
    'name' => 'bideo',
    'boss' => 'julia',
    'other_variable' => 
    array (
      'picture1' => 
      array (
        'name' => 'zee',
        'id' => 4,
      ),
      'picture2' => 
      array (
        'name' => 'izzy',
        'id' => 1,
      ),
    ),
  ),
)

晚编辑:

修改$other_variable数组的其他方法(在迭代过程结算时有效地将其清空)包括:

$category=array_map(function($v)use(&$other_variable){
    return $v+=['other_variable'=>array_shift($other_variable)];
},$category);

foreach($category as &$value){
    $value["other_variable"]=array_shift($other_variable);
}

显然,只有在您不打算使用$other_variable向下脚本或者您要声明该数组的副本供以后使用时,才应使用这些方法。