How to use in array in PHP

In this article, I will explain how the in_array() function can be used in PHP.
  • 1636

in_array() function in PHP

  • The in_array() function is used to search an array for a specific value exits in an array or not.
  • The in_array() function returns TRUE if the value is found in the array.
  • The in_array() function returns FALSE if the value is not found in the array.

Syntax

in_array(search,array,type)

Parameter

  • search search is required parameter. value to be search in array.
  • array array is required parameter. Determine array to be search.
  • type type is optional parameter. If it is set to true, the function checks the type of the search_value.

Example

The following example show to how the in_array() function can be used in PHP.

<html>

<body>

<h3 style="color: skyblue;">in_array() function example in PHP</h3>

    <?php

    $people = array("Arti", "Anamika", "Pallavi", "Pratibha", 25);

    if (in_array("25",$people, TRUE))

    {

    echo "Record found<br />";

    }

    else

    {

    echo "Record not found<br />";

    }

    if (in_array("Pratibha",$people, TRUE))

    {

    echo "Record found<br />";

    }

    else

    {

    echo "Record not found<br />";

    }

    if (in_array(25,$people, TRUE))

    {

    echo "Record found<br />";

    }

    else

    {

    echo "Record not found<br />";

    }

    ?>

</body>

</html>

 

Output

in arrayphp.jpg
You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.