我有這兩個數組,我需要組合兩個multidimensioal陣列,並設置一個作爲鍵
Array (
[0] => Column 1
[1] => Column 2
[2] => Column 3
)
Array (
[0] => Array (
[0] => 111
[1] => 222
[2] => 333
)
[1] => Array (
[0] => 444
[1] => 555
[2] => 666
)
)
組合成這個
Array (
[0] => Array (
[Column 1] => 111
[Column 2] => 222
[Column 3] => 333
)
[1] => Array (
[Column 1] => 444
[Column 2] => 555
[Column 3] => 666
)
)
這是我和它的作品,但我我確信它可以以一種更簡單的方式完成:
$values = array(array(1,2,3), array(4,5,6));
$fields = array('Column 1','Column 2','Column 3');
$i = 0;
$j = 0;
$l = 0;
$rows = array();
$columns = array();
foreach($values as $val) {
$rows[] = $val;
foreach ($fields as &$field) {
$columns[$j][$i] = $field;
$i++;
}
$i = 0;
$j++;
}
foreach($columns as $c){
$result[] = array_combine($c,$rows[$l]);
$l++;
}
我想如果可能的話清理它。我有時會遇到與陣列的糾纏,而這真的與我的頭腦混爲一談。 :-)