开发者

php while loop with condition check by array

I've two 开发者_如何转开发arrays suppose:

$a = array(1,2,3,4);
$b = array(3,4,5,6);

Now I've to do something while all $a element is exactly equals to array $b


array_diff()


have you tried array_diff()?


There are a few things you could do here in my mind.

A VERY simple and basic way would be to loop through and have some logic in there to check if $a[$val] == $b[$val] and if it is do something, otherwise not.

Like the good people above said, there is a function in PHP called array_diff() which computes the difference in arrays. The below example is taken from the PHP.net site.

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

So it depends on exactly what you want to do. If you wish to make your statement more clear then please do so and I will try to re-answer accordingly.

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜