我正在尝试从两组数组中平均划分数组
例如:
$arr1 = [a,b,c,d,e];
$arr2 = [1,2,3,4,5,6,7,8,9,10,11,12,13];
我尝试过的事情:
$arr1 = [a,b,c,d,e];
$arr2 = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$arrRes = [];
$key = 0;
for($i=0;$i<count($arr1);$i++){
$arrRes[$arr1[$key]][] = $arr2[$i];
$key++;
}
$key2 = 0;
for($k=0;$k<count($arr1);$k++){
$arrRes[$arr1[$key2]][] = $arr2[$key];
$key++;
$key2++;
if ($key == count($arr2)) {
break;
}
}
我希望得到输出:
[
"a" => [1,6,11],
"b" => [2,7,12],
"c" => [3,8,13],
"d" => [4,9],
"e" => [5,10]
]
但是我得到的实际输出是:
[
"a" => [1,6],
"b" => [2,7],
"c" => [3,8],
"d" => [4,9],
"e" => [5,10]
]
答案 0 :(得分:7)
另一种方法,仅使用1个循环(代码中的注释)...
$arr1 = ['a','b','c','d','e'];
$arr2 = [1,2,3,4,5,6,7,8,9,10,11,12,13];
// Create output array from the keys in $arr1 and an empty array
$arrRes = array_fill_keys($arr1, []);
$outElements = count($arr1);
// Loop over numbers
foreach ( $arr2 as $item => $value ) {
// Add the value to the index based on the current
// index and the corresponding array in $arr1.
// Using $item%$outElements rolls the index over
$arrRes[$arr1[$item%$outElements]][] = $value;
}
print_r($arrRes);
输出...
Array
(
[a] => Array
(
[0] => 1
[1] => 6
[2] => 11
)
[b] => Array
(
[0] => 2
[1] => 7
[2] => 12
)
[c] => Array
(
[0] => 3
[1] => 8
[2] => 13
)
[d] => Array
(
[0] => 4
[1] => 9
)
[e] => Array
(
[0] => 5
[1] => 10
)
)
答案 1 :(得分:3)
下面的代码段完全符合您的要求。它通过将第二个数组的长度除以第一个数组的长度来计算所得内部数组的最大长度(在您的情况下为13/5+1=3
)。然后,对于第一个数组中的每个元素,它从0到最大长度,并将该位置上第二个数组中的元素添加到结果数组中。如果位置超出范围,则退出内部for循环。
$arr1 = ['a', 'b', 'c', 'd', 'e'];
$arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$arrRes = [];
// create an empty result array of arrays
foreach($arr1 as $key){
// the keys are the values from the 1st array
$arrRes[$key] = [];
}
$maxLength = intval(count($arr2) / count($arr1)) + 1;
for($i = 0; $i < count($arr1); ++$i) {
for($j = 0; $j < $maxLength; ++$j) {
$pos = $j * count($arr1) + $i;
if($pos >= count($arr2)) {
break;
}
$arrRes[$arr1[$i]][] = $arr2[$pos];
}
}
上面的代码产生:
[
"a" => [1,6,11],
"b" => [2,7,12],
"c" => [3,8,13],
"d" => [4,9],
"e" => [5,10]
]
如果您想要这样的结果:
[
"a" => [1,2,3],
"b" => [4,5,6],
"c" => [7,8,9],
"d" => [10,11],
"e" => [12,13]
]
...然后此代码将执行此操作(主要区别在于获取位置并确定何时中断内循环):
$arr1 = ['a', 'b', 'c', 'd', 'e'];
$arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$arrRes = [];
foreach($arr1 as $key){
$arrRes[$key] = [];
}
$maxLength = intval(count($arr2) / count($arr1)) + 1;
$pos = 0;
for($i = 0; $i < count($arr1); ++$i) {
$arraysLeftAfter = count($arr1) - $i - 1;
for($j = 0; $j < $maxLength && $pos < count($arr2); ++$j) {
if($arraysLeftAfter > 0) {
$elCountAfter = count($arr2) - $pos - 1;
$myLengthAfter = ($j + 1);
$maxLengthAfter = floor(($elCountAfter / $arraysLeftAfter) + 1);
if($myLengthAfter > $maxLengthAfter) {
break;
}
}
$arrRes[$arr1[$i]][] = $arr2[$pos++];
}
}
答案 2 :(得分:0)
另一种方法:
$arr1 = ['a','b','c','d','e'];
$arr2 = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$i =0;
$res = array_fill_keys($arr1, []);
while(isset($arr2[$i])){
foreach($res as $k=>&$v){
if(!isset($arr2[$i])) break;
$v[] = $arr2[$i];
$i++;
}
}
print_r($res);
结果:
Array
(
[a] => Array
(
[0] => 1
[1] => 6
[2] => 11
)
[b] => Array
(
[0] => 2
[1] => 7
[2] => 12
)
[c] => Array
(
[0] => 3
[1] => 8
[2] => 13
)
[d] => Array
(
[0] => 4
[1] => 9
)
[e] => Array
(
[0] => 5
[1] => 10
)
)