1

I'm having troubles trying to create a certain array. Basicly, I have an array like this:

[0] => Array
    (
        [id] => 12341241
        [type] => "Blue"
    )

[1] => Array
    (
        [id] => 52454235
        [type] => "Blue"
    )

[2] => Array
    (
        [id] => 848437437
        [type] => "Blue"
    )

[3] => Array
    (
        [id] => 387372723
        [type] => "Blue"
    )

[4] => Array
    (
        [id] => 73732623
        [type] => "Blue"
    )

...

Next, I have an array like this:

[0] => Array
    (
        [id] => 34141
        [type] => "Red"
    )

[1] => Array
    (
        [id] => 253532
        [type] => "Red"
    )

[2] => Array
    (
        [id] => 94274
        [type] => "Red"
    )

I want to construct an array, which is a combination of the two above, using this rule: after 3 Blues, there must be a Red:

Blue1
Blue2
Blue3
Red1
Blue4
Blue5
Blue6
Red2
Blue7
Blue8
Blue9
Red3

Note that the their can be more Red's than Blue's, but also more Blue's than Red's. If the Red's run out, it should begin with the first one again.

Example: let's say there are only two Red's:

Blue1
Blue2
Blue3
Red1
Blue4
Blue5
Blue6
Red2
Blue7
Blue8
Blue9
Red1
...

...

If the Blue's run out, the Red's should append until they run out too. Example: let's say there are 5 Blue's, and 5 Red's:

Blue1
Blue2
Blue3
Red1
Blue4
Blue5
Red2
Red3
Red4
Red5

Note: the arrays come from mysql-fetches. Maybe it's better to fetch them while building the new array?

Anyway, the while-loops got to me, I can't figure it out...

Any help is much appreciated!

8
  • 1
    seems like very simple algorithm to me Commented Aug 19, 2014 at 9:54
  • So you already have an array of blues, and an array of reds, and the problem is just interleaving them? Commented Aug 19, 2014 at 9:54
  • Why the tag MySQL? If both arrays are coming from a MySQL db, you maybe can create the final array with SQL, but therefore you need to show us the db structure Commented Aug 19, 2014 at 9:55
  • 3
    @Reeno I have a feeling, trying to do this in MySQL would create unnecessary overhead on the DB. Doing it within PHP code is better I think, in this case Commented Aug 19, 2014 at 9:56
  • 1
    @binoculars: You want to traverse the arrays iterator-style, e.g. with each. This allows you to respond to the reds running out simply with reset($reds). Commented Aug 19, 2014 at 10:03

3 Answers 3

2

This is much easier than you (and others) seem to think:

$r = 0;
foreach($blues as $c => $v) {
    $out []= $v;
    if(($c + 1) % 3 == 0)
        $out []= $reds[$r++ % count($reds)];
}

$out = array_merge($out, array_slice($reds, $r));

The modulus cares for cycling, and the last line appends the rest of reds (if any) to the result.

https://ideone.com/cxANRW

Sign up to request clarification or add additional context in comments.

1 Comment

This is why I consult Stackoverflow... When my solution is taken up more lines than it should, there's always a smarter solution. Thanks a lot!
0
$blue = array (
[0] => Array
(
    [id] => 12341241
    [type] => "Blue"
)

[1] => Array
(
    [id] => 52454235
    [type] => "Blue"
)

[2] => Array
(
    [id] => 848437437
    [type] => "Blue"
)

[3] => Array
(
    [id] => 387372723
    [type] => "Blue"
)

[4] => Array
(
    [id] => 73732623
    [type] => "Blue"
));

$red = array(
[0] => Array
    (
        [id] => 34141
        [type] => "Red"
    )

[1] => Array
    (
        [id] => 253532
        [type] => "Red"
    )

[2] => Array
    (
        [id] => 94274
        [type] => "Red"
    )
);
$mixed = array();
$index = $b = $r = 0;
if (count($blue) > count($red)){
   $counter = 0;
   for ($i = 0; $i<count($blue) ; $i++){
      $mixed [$index] = $blue[$b];
      $b++;
      $index++;
      if ($r == count($red))
         $r=0;
      if ($counter == 3){
          $counter =0;
          $mixed[$index] = $red[$r];
          $r++;
      }
    $counter++;
   }
}
else {
     $counter = 0;
   for ($i = 0; $i<count($red) ; $i++){
      $mixed [$index] = $blue[$b];
      $b++;
      $index++;
      if ($counter == 3 or $b == count($blue)){
          $counter =0;
          $mixed[$index] = $red[$r];
          $r++;
      }
    $counter++;
   }
}

Comments

0
$blue_array = array(array('id'=>'121','type'=>'blue'),array('id'=>'122','type'=>'blue'),array('id'=>'123','type'=>'blue'),array('id'=>'124','type'=>'blue'),array('id'=>'125','type'=>'blue'),array('id'=>'125','type'=>'blue'));   
$red_array = array(array('id'=>'123','type'=>'red'),array('id'=>'123','type'=>'red'),array('id'=>'123','type'=>'red'),array('id'=>'123','type'=>'red'));
$result_array = array();
$blue_count = 1;
$red_count = 1;
// Go through $blue_array first
foreach($blue_array as $blue) {
    // Shove an item from $red_array for every 3 $blue_array items
    if($blue_count % 3 == 0) {
        $result_array[] = 'Blue' . $blue_count;
        if(count($red_array) > 0) {
            $temp = array_shift($red_array);
            $result_array[] = 'Red' . $red_count;
            $red_count++;
        }
    } else {
        $result_array[] = 'Blue' . $blue_count;
    }
    $blue_count++;
}
// Add remaining $red_array items to $result_array
foreach($red_array as $red) {
    $result_array[] = 'Red' . $red_count;
    $red_count++;
}
print_r($result_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.