I'm trying to find a string in an array and then return the index and check that index in another array to see if it matches (I'm looking for open times and matching close times in the arrays respectively).
The string might appear more than once in $openList, and it shouldn't stop checking until it's found a pair of matching times in both $openList and $closeList. array_search only finds the first occurrence so I'm having trouble creating a loop that works and is efficient (I'll be running this multiple times with different search values).
So far, I have something like:
$openList = array("10:00", "9:00", "10:15", "9:00", "2:30");
$closeList = array("2:15", "5:30", "10:30", "10:00", "3:00");
$found_key = false;
while (($key = array_search("9:00", $openList)) !== NULL) {
if ($closeList[$key] == "10:00") {
$found_key = true;
echo "found it at position ".$key;
break;
}
}
if (!$found_key) echo "time doesn't exist";
How can I fix it in an efficient way?
array_key_existsarray_key_existssearches the keys, not the content of the array itself. I'm looking for something that searches the array and returns the position it is at.array_keysas @David Nguyen suggests is the better function.