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;
}
}
精彩评论