Search code examples
phparraysxenforo

Evaluating array content (PHP, XenForo)


I'm having few troubles when evaluating an array data to filter some content. I'm sending to the template an array with few numbers (1,4,5,10,12,14,20 - for example) and then i want to filter if it has a specific value.

Let's say i want to know if it has '2' as a single value. What would be the best method?

I was using (strpos ($array, '2') !== false) but then i noticed that it checks if 2 is inside the array and not if it's just '2', so it returned true.

I need to be able to evaluate if '2' is specified as '2' and not if it's on the array string (like on '20' or '12').

PS: Although using xenforo(zend), any answer on php should be enough.

Hope you can help, Thanks!


Solution

  • If you want to know whether value is in array:

    in_array($needle, $haystack)

    If you want to get all values having given value:

    array_filter($haystack, $filteringFunction)