PHP check if a String is in multidimensional array

In this article we going to see PHP check if a String or value is in multidimensional-array.We going to use PHP array function.

Using PHP array function we going to get string or a value is present in multidimensional array.

Lets Start with this PHP check if a String is in multidimensional array step by step:

So Let’s with this, I have an array which contain the employees details with employeid,name ,amount.

These employees details are in the form called multidimensional-array. From this array, I have to find the specific value of employees. For this, I am going using to array function.

The array function i am going to use are as follow as:

- array_column()
- array_search()

So how we going to use this both array function to check string or value is contained in a multidimensional array. Suppose I want employeid for specific employe.

That is I want to check employee id is present in the array or not so I am going to use the first array_column. Array column what going to do is returns the values from a single column in the input array.

So for that, I have to pass the array key. After passing the array key it will return a single column.

Then I am going to use the array search function, using this I am going to get specific value or string check bypassing this string value if it is present in this multidimensional array, then it will return "Value Present in Array" or if it is not present then it will return "Value Not Found".

Below Is the example how we can achieve this.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>contains demo</title>
</head>
<body>
<?php
 
$search=Array
        (
        '0' => Array
           (
           'emp_id' => '100',
           'emp_name' => 'Peter',
           'emp_amount' => '900'
           ),
        '1' => Array
           (
           'emp_id' => '101',
           'emp_name' => 'John',
           'emp_amount' => '800'
           ),
        '2' => Array
           (
           'emp_id' => '106',
           'emp_name' => 'Jack',
           'emp_amount' => '500'
           )
        );
		
		if(array_search(100, array_column($search, 'emp_id')) !== False) {
			echo "Value Present in Array";
		} else {
			echo "Value Not Found";
		}
?>
</body>
</html>

What is PHP array_column() function?

PHP array_column() function returns the values from a single column in the input array.And syntax for array_column is array_column(array, column_key, index_key).

In which parameter array specifies the multi-dimensional array to use, parameter column_key an integer key or a string key name of the column of values to return, parameter index_key optional.

What is PHP array_search() function?

PHP array_search() function search an array for a value and returns the key.And syntax for array_search is array_search(value, array, strict).

In which parameter value specifies the value to search for, parameter array specifies the array to search in, parameter strict optional.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment