All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Search Multidimensional Array For Multiple Values

Last Updated : Mar 11, 2024

PHP Search Multidimensional Array For Multiple Values

In this tutorial we will show you the solution of PHP search multidimensional array for multiple values, here we passing our associative array to the foreach() loop there we using another foreach loop for passing specified key, value pairs to find then we checking key values with array values using if condition.

Found result will returns to php variable at last we printed each key value’s at webpage again using foreach loop.

Step By Step Guide On PHP Search Multidimensional Array For Multiple Values :-

Here we defined json array ‘$arr’ with three set of key, value pairs then we needs to find key values defined as array and stored on variable ‘$search_items’.

In search() method we passing array variables ‘$arr, $search_items’ and it returns will stored on variable ‘$res’ then finally we printing collected results of key values on webpage.

In search() method definition we defined foreach loop with array variable ‘$array’ there we iterating each key, value pairs within that we defined another foreach loop for iterate specified needs to found key values and using if condition we checking specified value with array values at last we returns result.

<?php
function search($array, $search_list) {
    $result = array();
    foreach ($array as $key => $value) {
        foreach ($search_list as $k => $v) {
        if (!isset($value[$k]) || $value[$k] != $v)
            {
               continue 2;
            }
        }
        $result[] = $value;
    }
    return $result;
}
$arr = array(
    1 => array(
'ID' => 4,
'name' =>'Alice',
'city' =>'UK'
    ),
    2 => array(
'ID' => 24,
'name' =>'Amit',
'city' =>'canada'
    ),
    3 => array(
'ID' => 31,
'name' =>'Bob',
'city' =>'canada'
    )
);
$search_items = array('city'=>'canada', 'ID'=>24);
$res = search($arr, $search_items);
foreach ($res as $var) {
    echo 'ID: ' . $var['ID'] . '<br>';
    echo 'Name: ' . $var['name'] . '<br>';
    echo 'City: ' . $var['city'] . '<br>';
}
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined search() method definition, we declared empty array ‘$result’ and in first foreach loop we using array ‘$array- it contains our json data of key, value pairs’.
  4. In this loop we iterating set of key, value pair one by one then within that we defined another foreach loop with array variable ‘$search_list – it contains needs to found data of key, value pair’.
  5. Here we iterating each key, value one by one then we checking array $arr’s value ‘$value’ of $search_list’s key ‘$k’ with $search_list’s value $v not equal or not set it will continue looping for check with other array values.
  6. If they matched with each other then it will stores that key, value pair to array variable ‘$result’ till found specified all key, value pairs after that loop gets exited and result returned.
  7. In array ‘$arr’ we defined json data of key, value pairs, it contains three sets of array at each array we specified three set of keys ‘ID,name,city’ with different values.
  8. The $search_items array variable defined for specify which are needs to find, here we specified ‘'city'=>'canada', 'ID'=>24’ values as needs to find in array ‘$arr’.
  9. The search() method return result will stored on variable ‘$res’ then again we defined foreach loop for print array of result using variable ‘$var’.

Conclusion :-

In conclusion now we are able to know how to search multiple values in multidimensional array using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will prints result of array ‘ID: 24, Name: Amit, City: canada’.

We can also change that defined json array ‘$arr’ data with some other values and needs to modify which is going to find values as per your changes on array ‘$arr’ then we gets result without any error.

I hope this tutorial on PHP search multidimensional array for multiple values helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪