-5

I have two arrays. i want to compare two array collection.

A Array Collection

Array
(
    [0] => Array
        (
           [id] => 334
        )
) 
Array
(
    [1] => Array
        (
           [id] => 338
        )
)
Array
(
    [2] => Array
        (
           [id] => 343
        )
)
Array
(
    [3] => Array
        (
           [item_id] => 376
        )
) 

B Array Collection:

Array
(
    [0] => Array
        (
           [id] => 343
        )
) 
Array
(
    [1] => Array
        (
           [id] => 334
        )
)

Output:

Exists
Not
Exists
Not
4

2 回答 2

1

为此使用 array_diff () 和 array_intersect()

于 2012-12-12T09:23:26.923 回答
0

在第一个数组成员之间迭代并使用in_array()函数检查每个成员与第二个数组。

$FirstArray = array(1, 2, 3, 4, 5);
$SecondArray = array(1, 3);

for($i = 0; $i < count($FirstArray); $i++){
    if(in_array($FirstArray[i], $SecondArray)){
        echo "Exists" . PHP_EOL;
    }
    else {
        echo "Doesn't Exists!" . PHP_EOL;
    }
}

它应该输出

Exists
Doesn't Exists!
Exists
Doesn't Exists!
Doesn't Exists!
于 2012-12-12T09:25:03.067 回答