如何将一个数组的子数组同步嵌套到另一个数组的子数组中?
How to synchronously nest subarrays from one array into subarrays of another?
我有 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
维护为父数组。
你期待这样的事情吗?这里我们使用 next
and current
函数来递增数组的内部指针并获取当前值。
foreach($category as &$value)
{
$value["other_variable"]=current($other_variable);
next($other_variable);
}
print_r($category);
使用 array_map()
同步迭代两个数组,您走在了正确的道路上,但遗憾的是,该函数将杀死您的第一级键。
The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
array_walk() 将是一个更清晰/更适合您修改 $category
数组的函数调用。 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
down 脚本或者您打算声明数组的副本供以后使用时才应使用这些方法。
我有 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
维护为父数组。
你期待这样的事情吗?这里我们使用 next
and current
函数来递增数组的内部指针并获取当前值。
foreach($category as &$value)
{
$value["other_variable"]=current($other_variable);
next($other_variable);
}
print_r($category);
使用 array_map()
同步迭代两个数组,您走在了正确的道路上,但遗憾的是,该函数将杀死您的第一级键。
The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
array_walk() 将是一个更清晰/更适合您修改 $category
数组的函数调用。 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
down 脚本或者您打算声明数组的副本供以后使用时才应使用这些方法。