Your code is simply more complicated than necessary.
Assuming what you are looking for is that the numbers at each position match (and not just that the array contains the same numbers), you can smooth your loop to unity.
<?php // Fill two arrays with random numbers as proof. $first_array = array(1000); $second_array = array(1000); for($i=0; $i<1000; $i++) $first_array[$i] = rand(0, 1000); for($i=0; $i<1000; $i++) $second_array[$i] = rand(0, 1000); // The loop you care about. for($i=0; $i<1000; $i++) if ($first_array[$i] != $second_array[$i]) echo "Error at $i: first_array was {$first_array[$i]}, second was {$second_array[$i]}<br>"; ?>
Using the code above, you will only execute a cycle 1000 times, unlike a cycle 1,000,000 times.
Now, if you just need to verify that the number appears or does not appear in arrays, use array_diff and array_intersect as follows:
<?php // Fill two arrays with random numbers as proof. $first_array = array(1000); $second_array = array(1000); for($i=0; $i<1000; $i++) $first_array[$i] = rand(0, 1000); for($i=0; $i<1000; $i++) $second_array[$i] = rand(0, 1000); $matches = array_intersect($first_array, $second_array); $differences = array_diff($first_array, $second_array); ?>
Jack Shedd Oct. 15 '10 at 14:35 2010-10-15 14:35
source share