Deleted the extra foreach

- Replace foreach with in_array
- Replace foreach with array_search