PHP:问题合并阵列 [英] PHP: Problem merging arrays
问题描述
确定我有这个功能(我为这个问题的答案<一个href=\"http://stackoverflow.com/questions/2113263/php-merge-add-multi-dimensional-array\">question)该合并的数组,像这样:
OK I have this function (I got as the answer to this question) that merges an array like so:
function readArray( $arr, $k, $default = 0 ) {
return isset( $arr[$k] ) ? $arr[$k] : $default ;
}
function merge( $arr1, $arr2 ) {
$result = array() ;
foreach( $arr1 as $k => $v ) {
if( is_numeric( $v ) ) {
$result[$k] = (int)$v + (int) readArray( $arr2, $k ) ;
} else {
$result[$k] = merge( $v, readArray($arr2, $k, array()) ) ;
}
}
return $result ;
}
用法
$basketA = array( "fruit" => array(), "drink" => array() ) ;
$basketA['fruit']['apple'] = 1;
$basketA['fruit']['orange'] = 2;
$basketA['fruit']['banana'] = 3;
$basketA['drink']['soda'] = 4;
$basketA['drink']['milk'] = 5;
$basketB = array( "fruit" => array(), "drink" => array() ) ;
$basketB['fruit']['apple'] = 2;
$basketB['fruit']['orange'] = 2;
$basketB['fruit']['banana'] = 2;
$basketB['drink']['soda'] = 2;
$basketB['drink']['milk'] = 2;
$basketC = merge( $basketA, $basketB ) ;
print_r( $basketC ) ;
输出
Array
(
[fruit] => Array
(
[apple] => 3
[orange] => 4
[banana] => 5
)
[drink] => Array
(
[soda] => 6
[milk] => 7
)
)
确定这个作品用1缺陷我无法弄清楚如何解决:
如果$ ARR1缺少的东西,$ ARR2有,也应该只使用从$ ARR2的价值,而是忽略了一起:
$basketA = array( "fruit" => array(), "drink" => array() ) ;
$basketA['fruit']['apple'] = 1;
$basketA['fruit']['orange'] = 2;
$basketA['fruit']['banana'] = 3;
$basketA['drink']['milk'] = 5;
$basketB = array( "fruit" => array(), "drink" => array() ) ;
$basketB['fruit']['apple'] = 2;
$basketB['fruit']['orange'] = 2;
$basketB['fruit']['banana'] = 2;
$basketB['drink']['soda'] = 2;
$basketB['drink']['milk'] = 2;
$basketC = merge( $basketA, $basketB ) ;
print_r( $basketC ) ;
输出
Array
(
[fruit] => Array
(
[apple] => 3
[orange] => 4
[banana] => 5
)
[drink] => Array
(
[milk] => 7
)
)
注意如何[苏打]是不是新的数组中,因为第一个数组没有它。
Notice how [soda] is not in the new array because the first array did not have it.
我怎样才能解决这个问题?
How can I fix this???
谢谢!
推荐答案
速战速决,改变合并()
函数如下:
Quick fix, change the merge()
function to look like this:
function merge( $arr1, $arr2 ) {
$result = array() ;
foreach( $arr1 as $k => $v ) {
if( is_numeric( $v ) ) {
$result[$k] = (int)$v + (int) readArray( $arr2, $k ) ;
} else {
$result[$k] = merge( $v, readArray($arr2, $k, array()) ) ;
}
}
foreach( $arr2 as $k => $v ) {
if( is_numeric( $v ) ) {
$result[$k] = (int)$v + (int) readArray( $arr1, $k ) ;
} else {
$result[$k] = merge( $v, readArray($arr1, $k, array()) ) ;
}
}
return $result ;
}
输出:
Array
(
[fruit] => Array
(
[apple] => 3
[orange] => 4
[banana] => 5
)
[drink] => Array
(
[soda] => 2
[milk] => 7
)
)
这也是值得注意的是, array_merge_recursive()
独行几乎是相同的:
It's also worth noticing that array_merge_recursive()
alone does almost the same:
$basketC = array_merge_recursive($basketA, $basketB);
输出:
Array
(
[fruit] => Array
(
[apple] => Array
(
[0] => 1
[1] => 2
)
[orange] => Array
(
[0] => 2
[1] => 2
)
[banana] => Array
(
[0] => 3
[1] => 2
)
)
[drink] => Array
(
[milk] => Array
(
[0] => 5
[1] => 2
)
[soda] => 2
)
)
所以,如果你想知道有多少橘子在 $ basketC
,你只需要做的:
So if you wanted to know how many oranges are in $basketC
, you would just have to do:
array_sum($basketC['fruit']['orange']); // 4
这样,你不需要使用任何hackish的,缓慢的,未经证实的自定义函数。
这篇关于PHP:问题合并阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!