Boris Boris - 29 days ago 9
PHP Question

PHP array_search exact match

I have the following array. Note that some times there are more params, sometimes there are less.

array(0 => 'param1: value1',
1 => 'param2: value2',
2 => 'param5: value5',
3 => 'param7: value7');


I need to put in variable the values of the params that im interested in, e.g. param1 and param7, so i decided to use array_search and then to value.

E.g.

$value7 = array_search('param7:', $arr1);
$avalue7 = explode(':', $arr1[$value7]);
$value7 = $aValue7[1]


However it does not work - array_search does not find any matches, most probably because it search for exact match. Any suggestions, or improvements are welcome.

Answer

Use preg_grep() to search in arrays. See http://php.net/manual/en/function.preg-grep.php

Comments