Php 要获取不在数组A中的新数组吗?

Php 要获取不在数组A中的新数组吗?,php,Php,我有两个阵列: $A = array('a','b','c','d') $c = array('b','c','e','f') 我想获得一个新数组,其中包含不在数组$a中的项。因此它将是: $result = array('e','f'); 因为“e”和“f”不在$A中。使用 打印_rarray _diff$c、$A;返回 使用 打印_rarray _diff$c、$A;返回 使用数组_diff执行此任务。由于有些恼人,它没有返回两个数组之间的所有差异。仅传递第一个数组中的元素,这些元素在作

我有两个阵列:

$A = array('a','b','c','d')
$c = array('b','c','e','f')
我想获得一个新数组,其中包含不在数组$a中的项。因此它将是:

$result = array('e','f');
因为“e”和“f”不在$A中。

使用 打印_rarray _diff$c、$A;返回

使用 打印_rarray _diff$c、$A;返回

使用数组_diff执行此任务。由于有些恼人,它没有返回两个数组之间的所有差异。仅传递第一个数组中的元素,这些元素在作为参数传递的任何其他数组中都找不到

$array1 = array('a','b','c','d');
$array2 = array('b','c','e','f');
$result = array_diff($array2, $array1);
使用数组_diff执行此任务。由于有些恼人,它没有返回两个数组之间的所有差异。仅传递第一个数组中的元素,这些元素在作为参数传递的任何其他数组中都找不到

$array1 = array('a','b','c','d');
$array2 = array('b','c','e','f');
$result = array_diff($array2, $array1);
PSEDOO通用实现代码

免责声明:不熟悉PHP,其他答案表明有很多更快的方法:

在第一个数组中循环:

// Array of results
array results[];

// Loop through all chars in first array
for i = 0; i < A.size; i++
{
    // Have we found it in second array yet?
    bool matched = false;

    // Loop each character in 2nd array
    for j = 0; j < C.size; j++
    {
        // If they match, exit the loop
        if A[i] == C[J] then
            matched = true;
            exit for;
    }

    // If we have a match add it to results
    if matches == true then results.add(A[i])

}
PSEDOO通用实现代码

免责声明:不熟悉PHP,其他答案表明有很多更快的方法:

在第一个数组中循环:

// Array of results
array results[];

// Loop through all chars in first array
for i = 0; i < A.size; i++
{
    // Have we found it in second array yet?
    bool matched = false;

    // Loop each character in 2nd array
    for j = 0; j < C.size; j++
    {
        // If they match, exit the loop
        if A[i] == C[J] then
            matched = true;
            exit for;
    }

    // If we have a match add it to results
    if matches == true then results.add(A[i])

}