脚本宝典收集整理的这篇文章主要介绍了使用PHP从3个数组构建一个数组,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
数组A:
Array ( [0] => 2015-09-13 [1] => 2015-09-14 [2] => 2015-09-15 [3] => 2015-09-16 [4] => 2015-09-17 [5] => 2015-09-18 [6] => 2015-09-19 )
数组B:
Array ( [0] => 1 [1] => 8 )
数组C:
Array ( [0] => Leaves-19 [1] => Shifts-18 [2] => Shifts-18 [3] => Shifts-18 [4] => Shifts-18 [5] => Shifts-18 [6] => Leaves-19 [7] => Leaves-19 [8] => Shifts-12 [9] => Shifts-12 [10] => Shifts-12 [11] => Shifts-12 [12] => Shifts-12 [13] => Leaves-19 )
期望的最终输出:
Array ( [0] => 2015-09-13|1|Leaves-19 [1] => 2015-09-14|1|Shifts-18 [2] => 2015-09-15|1|Shifts-18 [3] => 2015-09-16|1|Shifts-18 [4] => 2015-09-17|1|Shifts-18 [5] => 2015-09-18|1|Shifts-18 [6] => 2015-09-19|1|Leaves-19 [7] => 2015-09-13|8|Leaves-19 [8] => 2015-09-14|8|Shifts-12 [9] => 2015-09-15|8|Shifts-12 [10] => 2015-09-16|8|Shifts-12 [11] => 2015-09-17|8|Shifts-12 [12] => 2015-09-18|8|Shifts-12 [13] => 2015-09-19|8|Leaves-19 )
我迷失了,并且前进了.
这是逻辑:
>第一个参数是一个日期,它来自形式数组B.重复
6个条目后.
>第二个参数是用户ID.它在6个条目后发生变化并传递给下一个id.
>第3个参数是数组B的条目.
Oter信息:
>阵列的长度不同.
>数组A,计数6个条目.
>数组B,计算随机条目.
>数组C,是数组A x 2.
我已经尝试为我的阵列B和阵列A中的foreach做一个for,但是它不起作用.
希望我能得到任何帮助或暗示.
非常感谢.
$OutputArray = Array(); for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){ array_push($OutputArray,$a1[ $i % count($a1) ] . "|" . $a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]); } PRint_r($OutputArray);
你得到:
Array ( [0] => 2015-09-13|1|Leaves-19 [1] => 2015-09-14|8|Shifts-18 [2] => 2015-09-15|1|Shifts-18 [3] => 2015-09-16|8|Shifts-18 [4] => 2015-09-17|1|Shifts-18 [5] => 2015-09-18|8|Shifts-18 [6] => 2015-09-19|1|Leaves-19 [7] => 2015-09-13|8|Leaves-19 [8] => 2015-09-14|1|Shifts-12 [9] => 2015-09-15|8|Shifts-12 [10] => 2015-09-16|1|Shifts-12 [11] => 2015-09-17|8|Shifts-12 [12] => 2015-09-18|1|Shifts-12 [13] => 2015-09-19|8|Leaves-19 )
如果你想按顺序(预期):
$OutputArray = Array(); $max = max(count($a1),count($a3)); for($i=0; $i < $max; $i++){ array_push($OutputArray,$a1[$i%count($a1)] . "|" . $a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]); } print_r($OutputArray);
你得到:
Array ( [0] => 2015-09-13|1|Leaves-19 [1] => 2015-09-14|1|Shifts-18 [2] => 2015-09-15|1|Shifts-18 [3] => 2015-09-16|1|Shifts-18 [4] => 2015-09-17|1|Shifts-18 [5] => 2015-09-18|1|Shifts-18 [6] => 2015-09-19|1|Leaves-19 [7] => 2015-09-13|8|Leaves-19 [8] => 2015-09-14|8|Shifts-12 [9] => 2015-09-15|8|Shifts-12 [10] => 2015-09-16|8|Shifts-12 [11] => 2015-09-17|8|Shifts-12 [12] => 2015-09-18|8|Shifts-12 [13] => 2015-09-19|8|Leaves-19 )
以上是脚本宝典为你收集整理的使用PHP从3个数组构建一个数组全部内容,希望文章能够帮你解决使用PHP从3个数组构建一个数组所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。