2
我有兩個數組。我需要迭代這個數組並創建一個新數組。Foreach通過多維數組嵌套
我的代碼如下所示:
<?php
$lines = array(
array(0,0,0,0,0),
array(2,2,2,2,2),
array(0,1,2,1,0),
array(2,1,2,1,2)
);
$indexes = array(2,3,4,5,6);
foreach($lines as $l => $line) {
foreach($line as $d => $val) {
foreach($indexes as $i => $index) {
if($val == 0) {
$simbols[$l][$i] = $index - 1;
} else if ($val == 2) {
$simbols[$l][$i] = $index + 1;
} else {
$simbols[$l][$i] = $index;
}
}
}
}
var_dump($simbols);
?>
從這個代碼,我得到了這樣的結果:
$simbols = array(
array(1,2,3,4,5),
array(3,4,5,6,7),
array(1,2,3,4,5),
array(3,4,5,6,7)
);
但是,接下來我預計:
$simbols = array(
array(1,2,3,4,5),
array(3,4,5,6,7),
array(1,3,5,5,5),
array(3,3,5,5,7)
);
我怎麼能做到這一點結果?
請ellabortate在什麼和爲什麼 – NDM 2014-10-01 09:38:51