Boris
Boris

Reputation: 749

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.

Upvotes: 0

Views: 3832

Answers (2)

mimipc
mimipc

Reputation: 1374

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

Upvotes: 3

Naryl
Naryl

Reputation: 1888

why don't you use a associative array?

$arr1 = array(
    "param1" => "value1",
    "param2" => "value2",
    ...   //complete it to the desired number of params
);

echo $arr1['param7'];

Upvotes: 4

Related Questions