Replace foreach with array_search