Learn in_array php function in 1 Minute

Function name: in_array php function
There are few things which you need to keep in mind before start to understand this tutorial. You should have basic understanding of php arrays and bool values.

Syntax: in_array(search_term, array, bool);
Support: PHP (4, 5, 7)
Return Type: in_array php function only returns true or false.

Conditions:

  • search_term (necessary)
  • array (necessary)
  • bool (optional)

Description about in_array php function:

In this function we checks whether a specific value exists in an array or not.

search_term is what we need to search in an array. For example you have an array of packages that contains Yearly, Monthly, Daily. Now, you have an array right? What if you want to know that Daily is present in packages array or not. Then we will put ‘Daily’ in place of search_term parameter.

Array: Collection of information that we have in an array. Like we discussed above, about packages that was our array.

Bool: It’s an optional parameter on by default it is set to false, but if you’ll set it true it will also check your searched term according to data types in an array

Example 1:
In this example we have an array of names and we want to check name of ‘John’ if it exists in our array or not.

<?php 

$names = ['kevin', 'smith', 'aliza', 'john'];
$search_term = 'John';


if(in_array($search_term, $names)){
    echo("John successfully found");
}else{
    echo("Sorry, john not found");
}

?>

Output: Sorry, john not found

Useful Tip:in_array Function, check terms case sensitively. So, in our example John will not be consider as john. Click To Tweet

Example 2:
In this example we’ll check our php in_array function whether or not it allows us to place an array in search_term or not.

Now, we have two array of names. First one for search_term another one for an array in which we need to check.
As we discussed above search_term could be any of the data type

<?php
$names = array(array('shan', 'shah'), array('zubair, mahboob'));
$search_term = array('shan', 'shah');

if(in_array($search_term, $names)){
    echo('Shan Shah successfully found');
}else{
     echo("Sorry, Shan Shah not found");
}
?>

Output:Shan Shah successfully found

Example 3:
In this example we’ll check that, what does it means to have bool value true or false in our in_array php function.

As I told you above that, bool by default set as false which means (no strictly check of data types) but if we’ll set it to true it will strictly check data types.

We have an array of counter here, and we want to search ‘10’ in our array.

//Case1: without bool value true. It is by befault false we don't need to put false into our code.
$counter  = array(5, 8, 10);
$search_term = '10';

if(in_array($search_term, $counter)){
    echo('10 Found successfully');
}else{
    echo('Sorry, 10 not found');
}

Output: 10 Found successfully

//Case2: with bool value true.
$counter  = array(5, 8, 10);
$search_term = '10';

if(in_array($search_term, $counter, true)){
    echo('10 Found successfully');
}else{
    echo('Sorry, 10 not found');
}

Output: Sorry, 10 not found

Did you notice? We set search_term as string. So, in first case our function didn’t check the data type and in second case it did.

Hope you got too many issues which should be cleared now from this guide.

Learn in_array php function from Video Tutorial below:

 

Don’t forget to tweet it & subscribe for more tutorials:

Best Resource to learn PHP functions with in depth explanation. I just learned in_array within 1 minute from here. Click To Tweet

 

Let me know if you’ll have issues in code in comments. I’ll wait for your feedback.

Don’t forget to share.

You will also like to learn array_change_key_case in 1 minute click here to learn this function

4 Comments

  1. Babar Malik May 9, 2018
    • Shan Shah May 10, 2018
  2. Maria nawaz May 10, 2018
    • Shan Shah May 10, 2018

Add Comment