Sequence: get 3 values from 3 arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Sequence: get 3 values from 3 arrays

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Sequence: get 3 values from 3 arrays

Post 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>';
(#10850)
Post Reply