1

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

2
  • 3
    I'm not really understand the question... Commented Sep 9, 2011 at 7:45
  • It's not clear what you want to do. Do you want to compare single elements in $a against single elements in $b? Do you want to compare the whole arrays (then you would not need a while loop)? Your current statement sounds like you wanted to compare each element in $a against $b. That does not seem to be sense, an element is never equal to a whole array. Commented Sep 9, 2011 at 7:45

3 Answers 3

4

array_diff()

Sign up to request clarification or add additional context in comments.

Comments

1

have you tried array_diff()?

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.