find in php array code example
Example 1: array_search in php
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
$key = array_search('red', $array);
?>
Example 2: array search by key in php
$arr = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
function find($mot){
global $arr;
$ok=false;
foreach ($arr as $k => $v)
{
if($k==$mot){
return $v; $ok=true;
}
}
if(ok==false){ return -1; }
}
echo find("two");
Example 3: find element in arrat php
$userdb=Array
(
(0) => Array
(
(uid) => '100',
(name) => 'Sandra Shush',
(url) => 'urlof100'
),
(1) => Array
(
(uid) => '5465',
(name) => 'Stefanie Mcmohn',
(pic_square) => 'urlof100'
),
(2) => Array
(
(uid) => '40489',
(name) => 'Michael',
(pic_square) => 'urlof40489'
)
);
simply u can use this
$key = array_search(40489, array_column($userdb, 'uid'));
Example 4: search php array
$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'
)
);