Page 1 of 1

Sequence: get 3 values from 3 arrays

Posted: Sun Jul 29, 2012 11:41 am
by pedroz
$array_A = array ( a1, a2, a3, a4, a5, a6, a7 );

$array_B = array (b1, b2, b3, b4, b5 );

$array_C = array (c1, c2, c3 );


Is there any class or code which allows me to have series of 3 values each from $array_A, $array_B and $array_C?


eg:
$serie_1 = array (a1, b1, c1);
$serie_2 = array (a2, b2, c2);
$serie_3 = array (a3, b3, c3);
$serie_4 = array (a4, b4, a5);
$serie_5 = array (a6, a7);

(in this example $array_A has 7 values but it may be random, same scenario with $array_B and $array_C)


Thanks!

Re: Sequence: get 3 values from 3 arrays

Posted: Tue Jul 31, 2012 11:21 pm
by Christopher
There are probably several ways to do it. Here is one:

Code: Select all

$array_A = array ('a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7' );
$array_B = array ('b1', 'b2', 'b3', 'b4', 'b5' );
$array_C = array ('c1', 'c2', 'c3' );

$i = 0;
$serie = array();
do {
     $j = 0;
     if (isset($array_A[$i])) {
          $serie[$i][$j++] = $array_A[$i];
     }
     if (isset($array_B[$i])) {
          $serie[$i][$j++] = $array_B[$i];
     }
     if (isset($array_C[$i])) {
          $serie[$i][$j++] = $array_C[$i];
     }
} while (isset($serie[$i++]));

echo '<pre>' . print_r($serie, 1) . '</pre>';