Check if a value exists in multidimensional array in PHP

This tutorial will discuss about unique ways to check if a value exists in multidimensional array in php.

Table Of Contents

Using array_column() and in_array() functions

The array_column() function in PHP accepts a multidimensional array and a key name as arguments. It returns an array containing all the values of the specified key from all the sub arrays in multidimensional array.

So, we can use this to get an array of all the values of a specified column from a multidimensional array. Then we can pass that to the in_array() function along with a value, to check if the given values exists in it. If it returns true, then it means the specified values exists in the specified column in multidimensional array.

For example, in the below example, we will check if value ‘Ritika’ exists in column ‘Name’ of multidimensional array or not in PHP.
Let’s see the complete example,

<?php
// Create a multidimensional array
$arr = array(
        array('Name' => 'Mathew', 'City' => 'Tokyo'),
        array('Name' => 'John',   'City' => 'Delhi'),
        array('Name' => 'Mark',   'City' => 'London'),
        array('Name' => 'Ritika', 'City' => 'Las vegas') );

// The value that need to be checked
$name = 'Ritika';

// Get an array of all values in column 'name'
$userNames = array_column($arr, 'Name');

// Check if the value exists in the
// column 'Name' of multidimensional array
if (in_array($name, $userNames)) {
    echo "Yes, The Value exists in the array";
} else {
    echo "No, The Value does not exists in the array";
}
?>

Output

Yes, The Value exists in the array

Using in_array() function and foreach

In the previous example, we checked if a value exists in a particular column of a multidimensional array or not. But if we want to check if a value exist in any sub array of multidimensional array, then we can follow these steps.

  • Iterate over all the sub-arrays in multidimensional array.
  • For each sub array check if it contains the given value using the in_array() function.
  • As soon as a sub-array is encountered that contains the given value. Stop the iteration and return true.

Let’s see the complete example,

<?php
// Create a multidimensional array
$arr = array(
    array('Name' => 'Mathew', 'City' => 'Tokyo'),
    array('Name' => 'John',   'City' => 'Delhi'),
    array('Name' => 'Mark',   'City' => 'London'),
    array('Name' => 'Ritika', 'City' => 'Las vegas') );

// The value that need to be checked
$name = 'Ritika';

// Iterate over all the sub arrays in multidimensional array
foreach ($arr as $subArray) {
    // For each subarray, check if it contains the given value
    if (in_array($name, $subArray)) {
        echo "Yes, The Value exists in the array";
        break;
    }
}
?>

Output

Yes, The Value exists in the array

Summary

We learned about two ways to check if a value exists in a multidimensional array in PHP. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top