php example code : array_search function in php - searches for a given value & returns the corresponding key
array-search-example-in-php.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>php example code : array_search function in php - searches for a given value & returns the corresponding key</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Md Iqbal Hosan">
<meta name="title" content="php example code : array_search function in php - searches for a given value & returns the corresponding key">
<meta name="keywords" content="php, example, code, array, function, array_search">
<meta name="description" content="php example code : array_search function in php - searches for a given value & returns the corresponding key">
</head>
<body>
<h3>php example code : array_search function in php - searches for a given value & returns the corresponding key</h3>
<?php
/*
------------------------------------------------
let us make an array with some keys and values
------------------------------------------------
*/
$myArray = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");
echo "<p style='background:#aaa; color:red;'>Output of myArray</p>";
echo "<pre>";
print_r($myArray);
echo "</pre>";
/*
----------------------------------------
we want to search a value in the array
----------------------------------------
*/
$needle = "value2";
$result = array_search($needle, $myArray);
echo "<p style='background:#ccc; color:red;'>Output of needle : value2 using array_search function</p>";
echo "<pre>";
print $result;
echo "</pre>";
/*
-----------------------------------------------------------------------------------
if the needle is not exist in the array, array_search function returns false
try it yourself by changing the value of needle.
-----------------------------------------------------------------------------------
*/
?>
</body>
</html>

No comments:
Post a Comment
leave your comments here..