Skip to content
Advertisement

How to filter an array by a condition

I have an array like this:

JavaScript

Now I want to filter that array by some condition and only keep the elements where the value is equal to 2 and delete all elements where the value is NOT 2.

So my expected result array would be:

JavaScript

Note: I want to keep the keys from the original array.

How can I do that with PHP? Any built-in functions?

Advertisement

Answer

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