我有3个这样的数组。
$a = array(
0 => 'a1',
1 => 'a2',
2 => 'a3'
);
$b = array(
0 => 'b1',
1 => 'b2',
2 => 'b3'
);
$c = array(
0 => 'c1',
1 => 'c2',
2 => 'c3'
);
我想有这样的东西
$r = array(
0 => 'a1',
1 => 'b1',
2 => 'c1',
3 => 'a2',
4 => 'b2',
5 => 'c2',
6 => 'a3',
....
...
);
我怎样才能做到这一点,同时又能使用三个以上的输入数组?
EDIT:
我已经试过了
$a = array(
0 => 'a1',
1 => 'a2',
2 => 'a3',
3 => 'a4'
);
$b = array(
0 => 'b1',
1 => 'b2',
2 => 'b3'
);
$c = array(
0 => 'c1',
1 => 'c2',
2 => 'c3',
3 => 'c4',
4 => 'c5',
5 => 'c6'
);
$l['a'] = count($a);
$l['b'] = count($b);
$l['c'] = count($c);
arsort($l);
$largest = key($l);
$result = array();
foreach ($$largest as $key => $value) {
$result[] = $a[$key];
if(array_key_exists($key, $b)) $result[] = $b[$key];
if(array_key_exists($key, $c)) $result[] = $c[$key];
}
print_r($result);
输出 Array ( [0] => a1 [1] => b1 [2] => c1 [3] => a2 [4] => b2 [5] => c2 [6] => a3 [7] => b3 [8] => c3 [9] => a4 [10] => c4 [11] => [12] => c5 [13] => [14] => c6 )
这能用,但代码不好看。有谁有更好的解决方案吗?
解决方法我更新了@salathe的帖子,增加了一些动态功能。
function comb(){
$arrays = func_get_args();
$result = array();
foreach (call_user_func_array(array_map, $arrays) as $column) {
$values = array_filter($column, function ($a) { return $a !== null; });
$result = array_merge($result, $values);
}
return $result;
}
print_r(comb(null,$a,$b,$c,$d,....));
你可以使用 array_map()
函数来完成大部分的艰苦工作。
在本例中,循环中的代码只是从每个数组(null
如果没有对应的值),如果有一个值,则将它们追加到 $results
阵列。
例子
$result = array();
foreach (array_map(null, $a, $b, $c) as $column) {
$values = array_filter($column, function ($a) { return $a !== null; });
$result = array_merge($result, $values);
}
var_export($result);
产量
array (
0 => 'a1',
1 => 'b1',
2 => 'c1',
3 => 'a2',
4 => 'b2',
5 => 'c2',
6 => 'a3',
7 => 'b3',
8 => 'c3',
9 => 'a4',
10 => 'c3',
11 => 'c3',
12 => 'c3',
)
需要一些编码。
function customMerge() { $arrays = func_get_args(); $res = array(); $continue = true; while($continue){ $continue = false; for($j=0;$j<count($arrays); $j++){ if($pair = each($arrays[$j]){ if(is_numeric($pair[0]) $res[] = $pair[1]; else $res[ $pair[0] ] = $pair[1]; $continue = true; } } } return $res; } $res = customMerge($arr1, $arr2, $arr3, ...);
对不起,我之前的回答,误读了问题。 这是你需要的。
$arrays = array($a,$b,$c);
$new = array();
$count = count($arrays);
while(count($arrays) > 0) {
for($i = 0; $i < $count; $i++) {
if (isset($arrays[$i])) {
array_push($new, array_shift($arrays[$i]));
if (count($arrays[$i]) == 0) {
unset($arrays[$i]);
}
}
}
}
即使是大小不等的数组,你也会得到这样的结果:
$a = array(
0 => 'a1',
1 => 'a2',
2 => 'a3',
3 => 'a4'
);
$b = array(
0 => 'b1',
1 => 'b2',
2 => 'b3'
);
$c = array(
0 => 'c1',
1 => 'c2',
2 => 'c3',
3 => 'c4'
);
你也会得到结果
Array
(
[0] => a1
[1] => b1
[2] => c1
[3] => a2
[4] => b2
[5] => c2
[6] => a3
[7] => b3
[8] => c3
[9] => a4
[10] => c4
)
你可以这样做
function getMaxLength(array $arrays) {
$len = count($arrays);
if($len == 0) {
return 0;
}
$max = count($arrays[0]);
for($i = 1; $i < $len; $i++) {
$count = count($arrays[$i]);
if($count > $max) {
$max = $count;
}
}
return $max;
}
function combine() {
$arrays = func_get_args();
$maxLength = getMaxLength($arrays);
$combined = array();
for($i = 0; $i < $maxLength; $i++) {
foreach($arrays as $array) {
if(isset($array[$i])) {
$combined[] = $array[$i];
}
}
}
return $combined;
}
print_r(combine($a, $b, $c));
这个逻辑很烂... 不过还是可以用的
<?php
$a = array(
0 => 'a1',
1 => 'a2',
2 => 'a3'
);
$b = array(
0 => 'b1',
1 => 'b2',
2 => 'b3'
);
$c = array(
0 => 'c1',
1 => 'c2',
2 => 'c3',
3 => 'c4',
4 => 'c5'
);
$val=5; //Size of the largest array (largest array is c)
$d=array();
for($i=0;$i<$val;$i++)
{
$d[]=$a[$i];
$d[]=$b[$i];
$d[]=$c[$i];
}
//Clearing empty values
foreach ($d as $key=>$value)
if (empty($value))
unset($d[$key]);
//Consecutive Keys
$finalArray = array_values($d);
print_r($finalArray);
产出 :
Array ( [0] => a1 [1] => b1 [2] => c1 [3] => a2 [4] => b2 [5] => c2 [6] => a3 [7] => b3 [8] => c3 [9] => c4 [10] => c5 )
假设 count($a)=count($b)=count($c)
可以这样做。
<?php
$var = array();
for($i=0;$i<count($a);$i++)
{
array_push($var,$a[$i]);
array_push($var,$b[$i]);
array_push($var,$c[$i]);
}
print_r($var);
?>
这将导致:
Array ( [0] => a1 [1] => b1 [2] => c1 [3] => a2 [4] => b2 [5] => c2 [6] => a3 [7] => b3 [8] => c3 )
编辑:对于@蛋黄 我试了一下。
<?php
$a = array(
0 => 'a1',
1 => 'a2',
2 => 'a3',
3 => 'a4'
);
$b = array(
'x' => 'b1',
'y' => 'b4',
'z' => 'b3'
);
$c = array(
0 => 'c1',
'p' => 'c2',
2 => 'c3',
3 => 'c3',
'q' => 'c3',
5 => 'c3'
);
$d = array_merge($b,$a,$c);//place arrays in any order like $a,$b,$c or $b,$a,$c or $c,$b,$a
sort($d);
print_r($d);
?>
结果是:
Array ( [0] => a1 [1] => a2 [2] => a3 [3] => a4 [4] => b1 [5] => b3 [6] => b4 [7] => c1 [8] => c2 [9] => c3 [10] => c3 [11] => c3 [12] => c3 )
我不知道这是否能满足你的需求. 但是,我认为合并还是会发生的。但是,它并没有保存键。
可以通过排序来完成
$arr = array_merge($a,$b,$c);
foreach ($arr as $key => $val) {
$numsort[$key] = substr($val,1);
$charsort[$key] = substr($val,0,1);
}
array_multisort($arr, SORT_ASC, SORT_NUMERIC, $numsort, $arr, SORT_ASC, $charsort);
// result
Array
(
[0] => a1
[1] => b1
[2] => c1
[3] => a2
[4] => b2
[5] => c2
[6] => a3
[7] => b3
[8] => c3
)
你的情况只是合并一些特定的订单。所以明确的方法是
代码示例。
$merged = array_merge($a, $b, $c);
usort($merged, function($left, $right) {
if (substr($left, 1) == substr($right, 1)) {//if numbers equal check letters
return (substr($left, 0, 1) < substr($right, 0, 1)) ? -1 : 1;
}
return (substr($left, 1) < substr($right, 1)) ? -1 : 1;
});
print_r($merged);
输出。
Array
(
[0] => a1
[1] => b1
[2] => c1
[3] => a2
[4] => b2
[5] => c2
[6] => a3
[7] => b3
[8] => c3
)
有更通用的解决方案(所有第一行都是后续的,所有第二行都是后续的...)。
$result = call_user_func_array('array_merge', array_map(
function () {
return array_filter(func_get_args(), function ($element) {
return $element !== null;
});
} , $a, $b, $c)
);
print_r($result);
在找到这个页面之前,我回答了一个重复的问题。 我下面的解决方案使用的是 ...$row
的输入数据,但对于这个问题,只需将其切换为 ...$rows
对于 $a, $b, $c
. (此后我删除了我的另一个答案,并利用这个页面锤炼了重复的问题。)
我对功能单行本的想法如下(为了可读性,我把它拆开并贴上标签)。 需要注意的是,早期过滤将意味着尽量减少 "无用数据 "的处理,后期过滤将减少函数调用。
晚期过滤与贪婪lazyfalsey过滤:(演示)
var_export(
array_filter( #remove nulls
array_merge( #flatten the unpacked elements
...array_map( #unpack
null, #transpose
...$rows #transpose
) #unpack
) #flatten the unpacked elements
) #remove nulls
);
浓缩的单行本语法。(演示)
var_export(array_filter(array_merge(...array_map(null, ...$rows))));
中间过滤与贪婪的lazyfalsey过滤:(演示)
var_export(
array_merge( #flatten the unpacked elements
...array_filter( #remove nulls from transposing then unpack
array_map( #transpose
null, #transpose
...$rows #transpose
) #transpose
) #remove nulls from transposing then unpack
) #flatten the unpacked elements
);
或者做早期过滤... (用贪婪的lazyfalsey过滤)
编码。(演示)
var_export(
array_merge( #flatten the unpacked elements
...array_map( #unpack
function() { #transpose discarding nulls
return array_filter( #transpose discarding nulls
func_get_args() #transpose discarding nulls
); #transpose discarding nulls
}, #transpose discarding nulls
...$rows #transpose discarding nulls
) #unpack
) #flatten the unpacked elements
);
或早期过滤与贪婪lazyfalsey过滤,更多的是 "溅射":(演示)
var_export(
array_merge( #flatten the unpacked elements
...array_map( #unpack
function(...$v) { #transpose discarding nulls
return array_filter($v); #transpose discarding nulls
}, #transpose discarding nulls
...$rows #transpose discarding nulls
) #unpack
) #flatten the unpacked elements
);
这里是PHP7.4版本:(演示版)
var_export(
array_merge( #flatten the unpacked elements
...array_map( #unpack
fn(...$v) => array_filter($v), #transpose discarding nulls
...$rows #transpose discarding nulls
) #unpack
) #flatten the unpacked elements
);
浓缩为:
var_export(array_merge(...array_map(fn(...$v) => array_filter($v), ...$rows)));