I'm writing a PHP function to collect data from different positions in a given array. I've done it as follows which gives me what I require.
for ($a=9; $a < 13; $a++) {
$n = $arr[$a][2];
$p = $arr[$a][3];
$c = $p;
$v1[] = array($n,$p,$c);
}
for ($b=3; $b < 16; $b+=4) {
$h = $arr[6][$b];
$m = $arr[5][$b];
$l = $arr[4][$b];
$v2[] = array($h,$m,$l);
}
foreach ($v1 as $k=>$o) {
$r[] = array( 'na'=> $o[0], 'pr'=> $o[1], 'cu' => $o[2], 'val1' => $v2[$k][0], 'val2' => $v2[$k][1], 'val3' => $v2[$k][2]);
}
To get the output in this format, without repeating the fields:
[0] = > Array
[na] => Text
[pr] => 1
[cu] => Text
[val1] => 1
[val2] => 2
[val3] => 3
[1] = > Array
....
....
[3] = > Array
[na] => Text3
[pr] => 1-3
[cu] => Text-22
[val1] => 101
[val2] => 22
[val3] => 34
Is there a better, more efficient way to do this?
$c = $arr[$a][3]);
. Shouldn't that parenthesis be removed, and shouldn't the index be 4 instead of 3 (it's currently the same as the line above it) ? \$\endgroup\$