【问题标题】:PHP : array_intersect not giving expected resultPHP:array_intersect 没有给出预期的结果
【发布时间】:2020-05-29 00:13:44
【问题描述】:

我正在尝试计算 PHP 数组中 expectedactual 之间的匹配,我有这个...

$array = array(
    "item" => array(
        'expected' => array(
            '1' => 25,
            '2' => 4,
            '3' => 4,
        ),
        'color' => 'red',
        'actual' => array(
            '1' => 25,
            '2' => 4,
            '3' => 3,
        ),
    ),
);

foreach ($array as $key => $arrayItem) {

    $matches = array (
        'matches'  => count ( array_intersect ( $arrayItem['expected'], $arrayItem['actual'] ) ),
    );

}

echo "Matches = " . $matches['matches'];

我希望这会返回 2,但实际上它会返回 3。如果我更改下面示例中的值,那么它确实有效...

$array = array(
    "item" => array(
        'expected' => array(
            '1' => 25,
            '2' => 84,
            '3' => 4,
        ),
        'color' => 'red',
        'actual' => array(
            '1' => 25,
            '2' => 84,
            '3' => 3,
        ),
    ),
);

foreach ($array as $key => $arrayItem) {

    $matches = array (
        'matches'  => count ( array_intersect ( $arrayItem['expected'], $arrayItem['actual'] ) ),
    );

}

echo "Matches = " . $matches['matches'];

有人知道为什么顶级版本没有给我预期的结果吗?

【问题讨论】:

  • 在第一种方法中,它将expected 的第二个4actual 的第二个4 匹配。
  • 来自手册:array_intersect:“返回一个数组,其中包含 array1 中的所有值,其值存在于所有参数中。”
  • 也许你想要array_intersect_assoc
  • 我认为 array_intersect_assoc 可能是这个问题的答案。现在开始阅读

标签: php arrays array-intersect


【解决方案1】:

因为它返回一个数组,其中包含所有array1中的值,其值存在于所有参数中。

array_intersect(array $array1, array $array2[, array $... ]): array

https://www.php.net/manual/en/function.array-intersect.php

或许你可以从这个角度看清楚:

var_dump(array_intersect([25, 4, 4, 4], [25, 4, 3])); // [25, 4, 4, 4] 
// because the number `4` is in the second array!

var_dump(array_intersect([25, 4, 3], [25, 4, 4, 4])); // [25, 4]

【讨论】:

    【解决方案2】:

    计数实际上是正确的。

    在您的第二个示例中没有发生这种情况,因为您使用数字 84 和 4,但基本上这里是匹配项:

    $arrayItem['expected'][1]$arrayItem['actual'][1] 匹配(2525

    $arrayItem['expected'][2]$arrayItem['actual'][2] 匹配(44

    $arrayItem['expected'][3]$arrayItem['actual'][2] 匹配(44

    3 的计数是正确的。


    您可以通过将代码更改为以下内容来测试:

    $matches = array(
        'matches' => array_intersect ($arrayItem['expected'], $arrayItem['actual'])
    );
    
    var_dump($matches);
    

    您将在此处看到以下输出:

    array(1) {
        ["matches"] => array(3) {
            [1]=> int(25) 
            [2]=> int(4) 
            [3]=> int(4)
        }
    }
    

    【讨论】:

    • $arrayItem['expected'][2] 和 $arrayItem['expected'][3] 算作匹配 不正确。 See demo
    • 号码4expected 中被两次,这就是为什么4actual 中被检查 两次并得到匹配当然。 没有任何东西与自己匹配.
    【解决方案3】:

    它返回 2

    <?php
    
     $array = array(
        "item" => array(
            'expected' => array(
                '1' => 25,
                '2' => 84,
                '3' => 4,
            ),
            'color' => 'red',
            'actual' => array(
                '1' => 25,
                '2' => 84,
                '3' => 3,
            ),
        ),
    );
    
    echo count(array_intersect($array['item']['expected'],$array['item']['actual']));
    

    【讨论】:

    • OP 已经知道这一点。他的问题是为什么3 第一种方法。
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2021-01-06
    • 2018-12-08
    • 1970-01-01
    相关资源
    最近更新 更多