使用PHP从3个数组构建一个数组
发布时间:2020-12-13 21:54:36 所属栏目:PHP教程 来源:网络整理
导读:我在构造数组时遇到了一些问题. 数组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] = Shift
我在构造数组时遇到了一些问题.
数组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.重复 Oter信息: >阵列的长度不同. 我已经尝试为我的阵列B和阵列A中的foreach做一个for,但是它不起作用. 我不知道我需要从哪里开始. 希望我能得到任何帮助或暗示. 非常感谢. 解决方法
你可以使用
modulus operator
$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 ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |