在php中从多个数组连接多维数组
我有多个这样的数组在php中从多个数组连接多维数组,php,arrays,loops,multidimensional-array,Php,Arrays,Loops,Multidimensional Array,我有多个这样的数组 $data1 = [1,2,3]; $data2 = [a,b,c]; $data3 = [x,y,z]; 我想把它们都加入到这样的多个数组中 $data = [ 1 => ['data2' => a, 'data3' => x], 2 => ['data2' => b, 'data3' => y], 3 => ['data2' => c, 'data3' => z] ]; 我正在尝试循环,但不知道如何使它
$data1 = [1,2,3];
$data2 = [a,b,c];
$data3 = [x,y,z];
我想把它们都加入到这样的多个数组中
$data = [
1 => ['data2' => a, 'data3' => x],
2 => ['data2' => b, 'data3' => y],
3 => ['data2' => c, 'data3' => z]
];
我正在尝试循环,但不知道如何使它
$data = [];
for ($i=0; $i < count($desa); $i++) {
$data[] .= array ($desa[$i] => [
'info' => $info[$i],
'link' => $link[$i],
'sos' => $sos[$i],
'eko' => $eko[$i]
]);
}
return $data;
有人能帮我吗?我正在使用php。你可以在foreach循环中单独访问键值对
哦,你救了我一天:@AndiHaidar很乐意帮忙:等2分钟,投票选出正确的答案,但我会让它谢谢你有一个打字错误。应该是$data[$value]而不是$data[value]
// initalize the required output array
$data = [];
// loop over the data1
foreach ($data1 as $key => $value) {
// fill the output - key from loop will be used to
// access the values in data2 and data3
$data[$value] = array('data2' => $data2[$key],
'data3' => $data3[$key]);
}