我只是找不到正确的问题
非常多的合并数组
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
对此
$combined = [["name" => "temp1", "number" => 5], ["name" => "temp2", "number" => 7], ["name" => "temp3", "number" => 2]];
除了foreach以外,还有其他以最有效的方式进行操作的想法吗?
答案 0 :(得分:2)
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
foreach($arr1 as $key => $value)
{
$r[$key]['name'] = $value;
$r[$key]['number'] = $arr2[$key];
}
print_r($r);
答案 1 :(得分:1)
您可以使用foreach循环,
$result = [];
foreach($arr1 as $key => $value){
$resutl[] = array("name"=>$value,"number"=>$arr2[$key]);
}
答案 2 :(得分:1)
您可以执行以下代码来获取结果
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
$count = count($arr1);
$combined = array();
for($i=0;$i<$count;$i++){
$combined[$i]['name'] = $arr1[$i];
$combined[$i]['number'] = $arr2[$i];
}
答案 3 :(得分:1)