php - how to display particular array combination? -
i want display following array in output 246,357 donot process first subarray , remaining subarray combination should $array[1][0].$array[2][0].$array[3][0]
,similarly combination should $array[1][1].$array[2][1].$array[3][1]
$array=[ [0,1], [2,3], [4,5], [6,7] ];
i have written follwing code not bypass first subarray output 0246,1357
.plz help.
foreach($array $n) { $a.=$n[0]; $b.=$n[1]; } echo "$a".","."$b";
one way skip first element use flag variable $first
:
$first = true; foreach ($array $n) { if ($first) { $first = false; } else { $a .= $n[0]; $b .= $n[1]; } }
another way remove first element array, skipped over:
unset($array[0]);
or check key within foreach loop:
foreach ($array $k => $n) { if ($k > 0) { $a .= $n[0]; $b .= $n[1]; } }
yet way use array_shift()
, changes numeric keys in addition removing first element:
array_shift($array);
finally, because array contains consecutive integer keys starting 0, use normal loop:
for ($i = 1; $i < count($array); $i++) { $a .= $array[$i][0]; $b .= $array[$i][1]; }
Comments
Post a Comment