开发者

Search array element values similar to mysql like

开发者 https://www.devze.com 2023-02-04 04:11 出处:网络
I have to search array element values similar to mysql like. Array is as follows. $arraydata= array (0=> array(\'data\'=>1),

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开发者_JAVA百科('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


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;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消