N個のアイテムの配列が与えられた場合:
$arr = array('a', 'b', 'c', 'd', 'e', 'f');
M個のアイテムのグループでループする最もエレガントな方法は何ですか( NがMで割り切れると仮定)?
私は試した
foreach (array_chunk($arr, 2) as list($first, $second)) {
// do stuff with $first and $second
}
しかし、これにより構文エラーが発生しました。
言い換えれば、Tclで次のように見えるものをエミュレートしたいと思います。
set arr [a b c d e f]
foreach {first second} $arr {
// do stuff with $first and $second
}
今のところ、私は明白な手段に頼ってきました:
foreach (array_chunk($arr, 2) as $group) {
$first = $group[0];
$second = $group[1];
// do stuff with $first and $second
}
しかし、私は誰かがよりエレガントな方法を持っていることを望んでいます...