我有两个阵列。
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
我想合并到
Array (
[0] => Array (
[caption] => c one
[file] => photo one
)
[1] => Array (
[caption] => c two
[file] => photo two
)
)
或合并到
Array (
[0] => Array (
[0] => c one
[1] => photo one
)
[1] => Array (
[0] => c two
[1] => photo two
)
)
你是怎么做到的?
答案 0 :(得分:1)
您可以使用嵌套循环,只需跟踪您的密钥。
foreach ([$a, $b] as $array) {
foreach ($array as $text_key => $values) {
foreach ($values as $numeric_key => $value) {
$result[$numeric_key][$text_key] = $value;
}
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
如果数组没有比键值更多的共同点,那么这应该有效。
它循环一个数组并使用该键从另一个数组中获取值。
1.1.1
输出:
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
Foreach($a['caption'] as $key => $capt){
$new[$key]['caption'] = $capt;
$new[$key]['photos'] = $b['photos'][$key];
}
Var_dump($new);