我有一个基本函数,它接受两个数组并根据键合并它们。它适用于具有不同键但最终具有相同数量项的数组。功能很好。但是,它一次只能输入两个数组进行合并。
如何进行修改,以便合并n
个数组?
PHP
protected function arrayMergeKeys( $a, $b )
{
$retArr = array();
// I used min(countA, countB) so there will be no errors on
// unbalanced array item counts.
for ( $i = 0; $i < min( count( $a ), count( $b ) ); $i++ ) {
$retArr[ $i ] = array_merge( $a[ $i ], $b[ $i ] );
} // for end
return $retArr;
}
期望的结果是使用三个数组调用该函数:$merged_arrays = arrayMergeKeys($a, $b, $c);
输入
//$a
Array
(
[a] => 'new value'
[b] => 2
[c] => 3
)
//$b
Array
(
[1] => 'blah'
[2] => 'dud'
[3] => 697
)
//$c
Array
(
[pet] => 'dog'
[breed] => 'german shepher'
[age] => 100
)
输出
Array
{
[a] => 'new value'
[1] => 'blah'
[pet] => 'dog'
}
Array
{
[b] => 2
[2] => 'dud'
[breed] => 'german shepher'
}
Array
{
[c] => 3
[3] => 697
[age] => 100
}
答案 0 :(得分:1)
可以进一步优化,请再次重新检查
function arrayMergeKeys()
{
$retArr = array();
$arrayArgs = func_get_args();
//print_r($arrayArgs);
$minCount = count($arrayArgs[0]);
foreach($arrayArgs as $array){
if($minCount > count($array)){
$minCount = count($array);
}
}
// I used min(countA, countB) so there will be no errors on
// unbalanced array item counts.
for ( $i = 0; $i < $minCount; $i++ ) {
foreach($arrayArgs as $array){
$iCount = count($array);
for($j=0; $j<=$iCount; $j++)
{
if($i==$j){
$arrayKeys = array_keys($array);
if(isset($arrayKeys[$i])){
$retArr[ $i ][$arrayKeys[$i]] = $array[$arrayKeys[$i]];
break;
}
}
}
}
} // for end
return $retArr;
}
$a = Array
(
'a' => 'new value',
'b' => 2,
'c' => 3
);
$b = array
(
'1' => 'blah',
'2' => 'dud',
'3' => 697
);
$c = array
(
'pet' => 'dog',
'breed' => 'german shepher',
'age' => 100
);
print_r(arrayMergeKeys($a, $b, $c));
答案 1 :(得分:1)
保持原来的功能不受影响,你可以这样做:
function arrayMulitMergeKeys() {
$arrayArgs = func_get_args();
$c = count($arrayArgs);
$result = arrayArgs[0];
for($i = 1; $i < $c; $i++) {
$result = arrayMergeKeys($result, $arrayArgs[$i]);
}
return $result;
}
答案 2 :(得分:0)
这里有一些伪代码可以指向正确的方向。
function mergeArrays(a, b, c)
arrays = []
shortestLength = min(a.len, b.len, c.len)
for i in [0 .. shortestLength - 1]
arrays[i] = [ a[i], b[i], c[i] ];
return arrays
如果您将此作为练习,我建议您也执行以下操作:
mergeArrays(arr)
其中arr是需要合并的数组数组。
答案 3 :(得分:0)
Haven未经过测试,但这应该可行
protected function arrayMergeKeys(...$arrs) {
$retArr = array();
// I used min(countA, countB) so there will be no errors on
// unbalanced array item counts.
$countArr = array();
foreach ($arrs as $arr) {
$countArr[] = count($arr);
}
$min = min($countArr);
for ( $i = 0; $i < $min; $i++ ) {
$mergeArr = array();
foreach ($arrs as $arr) {
$mergeArr[] = $arr[$i];
}
$retArr[ $i ] = array_merge( $mergeArr );
} // for end
return $retArr;
}
答案 4 :(得分:0)
您可以简单地添加&#39;数组:
$a = array
(
'a' => 'new value',
'b' => '2',
'c' => '3'
);
//$b
$b = array
(
'1' => 'blah',
'2' => 'dud',
'3' => '697'
);
//$c
$c = array
(
'pet' => 'dog',
'breed' => 'german shepher',
'age' => 100
);
$result= $a+$b+$c;
print_r($result);
<强>输出强>
Array ( [a] => new value [b] => 2 [c] => 3 [1] => blah [2] => dud [3] => 697 [pet] => dog [breed] => german shepher [age] => 100 )