2
\$\begingroup\$

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?

\$\endgroup\$

migrated from stackoverflow.com Jun 27 '14 at 14:15

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ I think there's a mistake on this line: $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\$ – Joey Adams Jun 25 '14 at 16:11
  • \$\begingroup\$ yep, one value there has a manipulation on it, I deleted the function it calls, so my mistake with the ")" , changed the $c = $p; there also. \$\endgroup\$ – user3510209 Jun 25 '14 at 16:16
3
\$\begingroup\$

Should work (at least it doesn't look like a riddle anymore:)

for ($i = 0; $i < 4; $i++) {
    $a = $i + 9;
    $b = 3 + $i * 4;

    $r[] = array(
        'na'   => $arr[$a][2],
        'pr'   => $arr[$a][3],
        'cu'   => $arr[$a][3],
        'val1' => $arr[6][$b],
        'val2' => $arr[5][$b],
        'val3' => $arr[4][$b]
    );
}

if $r[..]['cu'] is some function result assign to function call directly.

\$\endgroup\$
0
\$\begingroup\$

From the answer of Scott Jungwirth, but a bit optimized :

for ($i=0; $i < 4; $i++)
{
    $a = $i + 9;
    $b = $i*4 + 3;
    $v1[] = array($arr[$a][2],$arr[$a][3],$arr[$a][3]);
    $v2[] = array($arr[6][$b],$arr[5][$b],$arr[4][$b]);
    $r[] = array(
                 'na'   => $o[0],
                 'pr'   => $o[1],
                 'cu'   => $o[2],
                 'val1' => $v2[$i][0],
                 'val2' => $v2[$i][1],
                 'val3' => $v2[$i][2],
                );
}

And i think that replacing the $a and $b variables in the brackets would make the code unreadable. So all credits for Scott Jungwirth, just saying that if you can avoid some useless variable assignment (ie reduce the used memory/time), try to !

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.