Skip to content
Advertisement

PHP – Check if value exists in array but only for specific key

I have an array and I am checking if a value exists in the array using in_array(). However, I want to check only in the ID key and not date.

JavaScript

So in this example, the condition should not be met since 25 exists in date, but not in ID.

JavaScript

Advertisement

Answer

To directly do this, you need to loop over the array.

JavaScript

If you need to do this for several IDs, it is better to convert the array to a map and use isset.

JavaScript

This will also allow you to look up any value in the map cheaply by id using $map[$key].

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement