//array_search
$result = array_search("apple", $fruit_array); // return index or false
//in_array
$result = in_array("apple", $fruit_array); // return true or false
//array_search
$result = array_search("apple", $fruit_array); // return index or false
//in_array
$result = in_array("apple", $fruit_array); // return true or false
array_search ( mixed $needle , array $haystack , bool $strict = false ) : int|string|false
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
// Search a partial string matching in array elements
function array_search_partial($arr, $keyword) {
foreach($arr as $index => $string) {
if (strpos($string, $keyword) !== FALSE)
return $index;
}
}
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}
$userdb = array(
array(
'uid' => '100',
'name' => 'Sandra Shush',
'pic_square' => 'urlof100'
),
array(
'uid' => '5465',
'name' => 'Stefanie Mcmohn',
'pic_square' => 'urlof100'
),
array(
'uid' => '40489',
'name' => 'Michael',
'pic_square' => 'urlof40489'
)
);