Not in Array PHP (With Advanced Program Examples)

This topic is all about identifying how to use the functions in_array() and not in array in PHP. This will deepen your knowledge about PHP programming by learning the in_array function and checking the not in_array.

Basically, we will first discover what is the…

Not in array PHP

The in_array() function is one of the built-in functions of PHP. This function will help programmers distinguish if an element exists in an array or not. So the not in_array function now confirms that a certain value or iterable is not included in the given array.

In simple words, we use the in_array to check if a value in an array exists and use the not in_array (!in_array) to know otherwise.

Syntax

bool in_array($val, $array_name, $mode)

Return value

Because the boolean parameter has the true or false value, the PHP in_array() function can return either of the following:

  • True is the return value if the $value is present in the array.
  • But the function will return false if the element is not in the array.

Parameters

The PHP in_array function takes the following parameters to execute its function. However, please note that two of them are required while the other 1 is not.

$val

The $val is a required parameter because it specifies the value of an element in an array. This specification applies to “what to search” purposes as this parameter can have different kinds of elements.

Therefore, the elements in the $val parameter can include strings, integers, floats, etc. However, the search implementation of this parameter is case-sensitive if its elements were pure strings.

$array_name

Similar to the $val parameter, the $array_name is also a required parameter of the PHP not in_array() function. The $val parameter is used to specify “what to search for,”. The $array_name parameter on the other hand, is used to search for an array.

Therefore, the reason why both$val and $name_array are required is that they serve as basis to search for the element’s type and its name.

$mode

In contrast to the aforementioned parameters, the $mode parameter of PHP’s not in_array() function is only optional. It is a boolean parameter, which means true or false. This tells the program the mode of the element that we want to search for.

For instance, when this parameter is set to true, the function then searches for a specific value of a similar type as defined. Additionally, remember that this parameter’s default value is false.

Approach

The main approach of the PHP not in_array function is to know if a specific value is not in an array. As it says in its return values, the main purpose of the PHP in_array() function is to find out if a specific $value is in an array or not. 

Let us give more emphasis to the approach of PHP not in_array() function with this sample program:

Sample Code

<?php
  $sample = array(100, 65, 70, 87);
  if (in_array("100", $sample))
  {
    echo "100 is present in the array.";
  }
  else
  {
    echo "100 is not in the array.";
  }
?>

Output

100 is present in the array.

The example above shows the simple approach of PHP in_array implementation. It has an example array of (100, 65, 70, 87) and to check if the value “100” exist in the given array, we use the if and else statement to implement the (in_array(“100”, $sample)) condition.

As an explanation, if the if statement is true, the program will display an output saying “100 is present in the array”, but if it is false, the program will pursue the alternative and display the “100 is not in the array.” as the output.

Example Program for PHP not in_array:

Now let us try the PHP not in_array function with string values in an array. Let us see how will this function deal with another set or array in the example below.

Sample Code

<?php
$sample = array("Windows", "Linux", "Mac");
if (!in_array("Blueberry", $sample)) {
    echo "Your OS does not exist.";
}
else {
	echo "Your OS does exist.";
}
?>

Output

Your OS does not exist.

As seen in the sample code, we have the array with string values and we test it with the PHP not in_array() function. The PHP not in_array is written as !in_array when we apply it to the program.

The same as the first example, we use the if and else statements to indicate the condition, but this time, we use the !in_array() function in it. Additionally, you will notice that the value “Blueberry” is not present in the (“Windows”, “Linux”, “Mac”) array. Therfore the program displays the output “Your OS does not exist.”.

Example Program in_array():

This time let us have an example where we apply the in_array() function plainly. The sample code below will add more clarification with the difference between using plain in_array function and not in-array in PHP.

Sample Code

<?php
$sample = array("Windows", "Linux", "Mac");
if (in_array("Windows", $sample)) {
    echo "Your OS does exist.";
}
else {
	echo "Your OS does not exist.";
}
?>

Output

Your OS does exist.

As you can see in this example, we have the same content of array and scenario as the former example. The only thing is, now, we would like to test how the function will react if we set the “Windows” which is also present in the (“Windows”, “Linux”, “Mac”) array.

As a result, the program confirms that the Windows OS is exist along with the given array.

Example program in_array() with strict:

To use the PHP not in_array function, we use it with specific boolean value which is true. This implementation activates the strict method where it checks if the specific value (string) does exist in the array. Take a good look at this example,

Sample Code

<?php
$sample = array( 1, 2, 3, 4, "5");

if (in_array( "5", $sample, true)) {
    echo "\"5\" was found with strict search.";
}
else {
	echo "\"5\" was not found.";
}
?>

Output

"5" was found with strict search.

Out of the array set given, there is a string included. In this example we use the $mode parameter specifically the true value to emphasize that we wants to strictly search for the “5” value.

As a result, the output confirms that “”5″ was found with strict search.”.

Now that we have tried multiple sorts of using the PHP not in_array function with a variety of array sets, how about we use it in multiple sets of array?

Example program in_array() with an array as needle:

In this example, you will be able to know how the PHP not in_array function works with multiple sets of array.

Sample Code

<?php
$sample = array(array('a', 'b'), array('c', 'd'), 'e');

if (in_array(array('a', 'b'), $sample)) {
    echo "'a' and 'b' was found.<br/>";
}

if (in_array(array('c', 'a'), $sample)) {
    echo "'c' and 'a' was found.<br/>";
}

if (in_array('e', $sample)) {
    echo "'e' was found.";
}
?>

Output

'a' and 'b' was found.
'e' was found.

More About Array

Conclusion

As a conclusion, it is proven that the PHP not in_array function can be applicable in multiple scenarios or conditions. This function can also be in form of plain in_array implementation.

Furthermore, the examples above concludes that the function is very useful in variety of ways, especially when dealing with multiple sets of arrays or with different types of values within an array.

I know that you can also conclude that this topic has covered everything that you need to understand what is a PHP not in_array function and its correct implementation. If you need more emphasis regarding this, you can always leave a comment.

Hope to see you again, have a good day.

Leave a Comment