How To Use Array Search Function With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

We can get specific key value from an array in PHP by using array_search() function.An array_search() is in-built function in php and array_search() is used to get specified values from given array. he array_search() function search an array for a value and returns the key.

get specific key value from an array in PHP

Syntax for array_search() Function

array_search(value, array, strict)

 

Parameter   Description

value   Specifies the value to search for
array   Specifies the array to search in
strict  If this parameter is set to TRUE, then this function will search for identical elements in the array. Possible values:
          true
          false - Default


Example(1)

<?php
$a=array("a"=>"Kiran","b"=>"Shiva","c"=>"Rajesh");
echo "<h1>". array_search("Kiran",$a)."</h1>";
?>



Example(2)

$b=array("a"=>"50","b"=>5,"c"=>"34");
echo "<h1>". array_search(5,$b,true)."</h1>";



Complete code of array_search()  function.

<!DOCTYPE html>
<html>
<head>
    <title>How to use array_search() function with example in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>PHP array_search() function </h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        $a=array("a"=>"Kiran","b"=>"Shiva","c"=>"Rajesh");
        echo "<h1>". array_search("Kiran",$a)."</h1>";

        $b=array("a"=>"50","b"=>5,"c"=>"34");
        echo "<h1>". array_search(5,$b,true)."</h1>";
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post