Rahul
Rahul

Reputation: 2731

Search array element values similar to mysql like

I have to search array element values similar to mysql like.

Array is as follows.

$arraydata=
  array (0=> array('data'=>1),
  1=> array('data'=>'1|5'),
  2=>array('data'=>'2|3'),
  3=>array('data'=>'1|5|6'),
  4=>array('data'=>'1|5|6|7'),
  5=>array('data'=>'2|3|4'),
  6=>array('data'=>'2|3|4|8')
);

If want to search "1" in array element 'data' values then the output should be return element 0,1,3,4. If search "2" then output should be return element 2,5,6

Upvotes: 1

Views: 2880

Answers (1)

Savetheinternet
Savetheinternet

Reputation: 491

There is an optional search parameter in array_keys.

$array = Array(0 => 1, 1 => 0, 2 => 1);
print_r(array_keys($array, 1));

But you appear to be using multi-dimensional arrays -- I'm not sure. You'd be better off using a foreach instead.

$results = Array();
foreach($arraydata as $key => $value) {
  if(strpos($value['data'],'1') !== false) {
    $results[] = $key;
  }
}

Upvotes: 1

Related Questions