再会!我在合并/组合/连接数组时出错。
例如我有数组,
$array1 [] = array(
'var1' => $var1,
'var2' => $var2,
'var3' => $var3,
);
$array2 [] = array(
'var4' => $var4,
'var5' => $var5,
'var6' => $var6,
);
这些数组内部有多个记录,但记录数相同。当我对它们进行 var_dump 时,结构如下所示:
//array1
array(3){
[0]=>array(3){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
}
[1]=>array(3){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
}
[2]=>array(3){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
}
}
//array2
array(3){
[0]=>array(3){
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
[1]=>array(3){
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
[2]=>array(3){
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
}
我想添加两个数组
$array_combined [] = array(
'var1' => $var1,
'var2' => $var2,
'var3' => $var3,
'var4' => $var4,
'var5' => $var5,
'var6' => $var6,
);
我认为 var_dump(ed) 时的结构应该是这样的:
//array_combined
array(3){
[0]=>array(6){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
[1]=>array(3){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
[2]=>array(3){
["var1"]=> string (4) "var1"
["var2"]=> string (4) "var2"
["var3"]=> string (4) "var3"
["var4"]=> string (4) "var4"
["var5"]=> string (4) "var5"
["var6"]=> string (4) "var6"
}
}
我试过:
$array_combine = array_combine($array1, $array2);
$array_merge = array_merge($array1, $array2);
$array_merge_recursive = array_merge_recursive($array1, $array2);
但我不断收到此错误:
注意:未定义的索引:D:\xampp\htdocs\vra\GetVM 中的 cmax - Copy.php 位于第 365 行
我在那里发布的代码与我的真实代码相似。
第 365 行包含 echo ( $record['var4'] )。
我正在做这样的事情来输出组合/合并的数组。
$array_merge = merge_array($array1, $array2)
foreach ( $array_merge as $record ) {
echo ( $record['var1'] );
echo ( $record['var2'] );
echo ( $record['var3'] );
echo ( $record['var4'] );//this is were the error occurs
echo ( $record['var5'] );//same error here
echo ( $record['var6'] );//same error here
}
尝试使用 array_combine()、array_merge_recursive 和 $combined_array = $array1 + $array2