Creating array from two arrays
Posted
by
binoculars
on Stack Overflow
See other posts from Stack Overflow
or by binoculars
Published on 2014-08-19T09:52:40Z
Indexed on
2014/08/19
10:20 UTC
Read the original article
Hit count: 160
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!
© Stack Overflow or respective owner