从两个数组创建数组

时间:2014-08-19 09:52:40

标签: php mysql arrays

我在尝试创建某个阵列时遇到了麻烦。基本上,我有一个这样的数组:

[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"
    )

...

接下来,我有一个这样的数组:

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

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

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

我想构建一个数组,这是上面两个的组合,使用这个规则:在3个蓝调之后,必须有一个红色:

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

请注意,他们的红色比蓝色更多,但蓝色也比红​​色更多。如果Red's耗尽,它应该从第一个开始。

示例:假设只有两个Red:

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

...

如果蓝色用完了,红色应该追加,直到它们用完为止。 例如:假设有5个蓝色和5个红色:

Blue1
Blue2
Blue3
Red1
Blue4
Blue5
Red2
Red3
Red4
Red5

注意:数组来自mysql-fetches。也许在构建新阵列时获取它们会更好?

无论如何,while-loops到了我身边,我无法理解......

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

这比你(和其他人)想象的要容易得多:

$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));

模数关心循环,最后一行将剩余的红色(如果有的话)附加到结果中。

https://ideone.com/cxANRW

答案 1 :(得分: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++;
   }
}

答案 2 :(得分: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);