0

i have something like below :

$array1 = array('first_name'=>'tom','last_name'=>'jackson','city'=>'london');
$array2 = array('last_name'=>'jackson','city'=>'london','first_name'=>'tom');
$array3 = array('city'=>'london','first_name'=>'tom','last_name'=>'jackson');
$array4 = array('last_name'=>'jackson','first_name'=>'tom','city'=>'london');

I want to check if all 4 arrays have same key with corresponding values. In above case it must return true as all values with its corresponding keys are same. I can do this using 2 for loops but i am wondering if there any other short cut method to reduce execution time.

Thank you in advance.... :-)

1
  • 1
    Please specify programming language on tags and question Commented Aug 23, 2011 at 11:07

1 Answer 1

1

If all arrays equal $array1 then they are all equal:

$array1 == $array2 && $array1 == $array3 && $array1 == $array4

This functions does the same:

function arrays_equal($arrays) {
    $arrays = func_get_args();
    for ($i = 1, $l = count($arrays); $i < $l; ++$i) {
        if ($arrays[0] != $arrays[$i]) {
            return false;
        }
    }
    return true;
}

if (array_equals($array1, $array2, $array3, $array4)) {
    // all arrays are equal (both keys and values)
}
Sign up to request clarification or add additional context in comments.

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.