PHP:合并数组时出现问题
好的,我有这个函数(我得到了这个问题的答案)合并一个数组,如下所示:
函数
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
)
)
注意 [soda] 不在新数组中,因为第一个数组没有拥有它。
我该如何解决这个问题???
谢谢!!!
OK I have this function (I got as the answer to this question) that merges an array like so:
Functions
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 ;
}
Usage
$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 ) ;
Output
Array
(
[fruit] => Array
(
[apple] => 3
[orange] => 4
[banana] => 5
)
[drink] => Array
(
[soda] => 6
[milk] => 7
)
)
OK this works with 1 flaw I cannot figure out how to fix:
if $arr1 is missing something that $arr2 has, it should just use the value from $arr2 but instead omits it all together:
Example
$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 ) ;
Output
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???
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速修复,将
merge()
函数更改为如下所示:输出:
还值得注意的是,
array_merge_recursive()
单独执行几乎相同的操作:输出:
所以如果你想知道
$basketC
中有多少个橙子,你只需要做:这样你就不需要使用任何黑客行为、缓慢且未经证实的自定义函数。
Quick fix, change the
merge()
function to look like this:Output:
It's also worth noticing that
array_merge_recursive()
alone does almost the same:Output:
So if you wanted to know how many oranges are in
$basketC
, you would just have to do:This way you don't need to use any hackish, slow and unproved custom function.