Php 从两个数组创建数组
我在尝试创建某个数组时遇到问题。基本上,我有这样一个数组:Php 从两个数组创建数组,php,mysql,arrays,Php,Mysql,Arrays,我在尝试创建某个数组时遇到问题。基本上,我有这样一个数组: [0] => Array ( [id] => 12341241 [type] => "Blue" ) [1] => Array ( [id] => 52454235 [type] => "Blue" ) [2] => Array ( [id] => 848437437
[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"
)
接下来,我有一个这样的数组:
[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
请注意,它们的颜色可以是红色多于蓝色,但也可以是蓝色多于红色。如果红色用完了,应该从第一个开始
示例:假设只有两个红色:
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抓取。也许在构建新阵列时获取它们更好
不管怎么说,当我遇到循环时,我想不出来
非常感谢您的帮助 $blue=数组(
$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++;
}
}
[0]=>阵列
(
[id]=>12341241
[类型]=>“蓝色”
)
[1] =>阵列
(
[id]=>52454235
[类型]=>“蓝色”
)
[2] =>阵列
(
[id]=>848437437
[类型]=>“蓝色”
)
[3] =>阵列
(
[id]=>387372723
[类型]=>“蓝色”
)
[4] =>阵列
(
[id]=>73732623
[类型]=>“蓝色”
));
$red=数组(
[0]=>阵列
(
[id]=>34141
[类型]=>“红色”
)
[1] =>阵列
(
[id]=>253532
[类型]=>“红色”
)
[2] =>阵列
(
[id]=>94274
[类型]=>“红色”
)
);
$mixed=array();
$index=$b=$r=0;
如果(计数($蓝色)>计数($红色)){
$counter=0;
对于($i=0;$i来说,这比你(和其他人)想象的要容易得多:
$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));
模数用于循环,最后一行将剩余的红色(如果有)附加到结果中
对于meSo来说,这似乎是一个非常简单的算法。你已经有了一个蓝色数组和一个红色数组,问题是它们只是交错在一起?为什么标记MySQL?如果两个数组都来自MySQL数据库,你可能可以用SQL创建最后一个数组,但是你需要向我们展示数据库structure@Reeno我有一种感觉,试着去做MySQL中的s会在数据库上产生不必要的开销case@binoculars:您希望遍历数组迭代器样式,例如使用each
。这允许您只需使用reset($reds)即可响应即将用完的red
。这就是我咨询Stackoverflow的原因……当我的解决方案占用的行数超过它应该占用的行数时,总会有更智能的解决方案。非常感谢!