2

好的,我有这个合并数组的函数(我得到了这个问题的答案),如下所示:

职能

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] 不在新数组中,因为第一个数组没有它。

我怎样才能解决这个问题???

谢谢!!!

4

1 回答 1

2

快速修复,将merge()函数更改为如下所示:

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()单独做几乎相同:

$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,您只需执行以下操作:

array_sum($basketC['fruit']['orange']); // 4

这样你就不需要使用任何骇人听闻的、缓慢的和未经证实的自定义功能。

于 2010-01-22T18:58:47.053 回答