Change language

How to search by multiple keys = & gt; value in PHP array?

Approach:for each array within the array, iterate over the search array, and if any value of the search key does not match the corresponding value of the array key, we discard that array and continue the process for the next array. Let’s better understand this with an example:
Suppose we want to find information about a student from a student list that contains a student in another section, so in this case RollNo alone may not produce the correct output. Therefore, we will need to find two values ​​key = > in the list, which are equal to rollNO and section.Example :  
// PHP search program several
// key = > pairs of values ​​in an array function search ( $array , $search_list ) { // Create array of results $result = array (); // iterate over each element of the array foreach ( $array as $key = > $value ) { // Loop over each search term foreach ( $search_list as $k = > $v ) { // If no array element occurs // search condition then continue // to next element if (! isset ( $value [ $k ]) || $value [ $k ]! = $v ) { // Skip two loops continue 2; } } // Add array element key to // array of results $result [] = $value ; } // Return the result return $result ; }  
// Multidimensional array for the list of students $arr = array ( 1 = > array ( ’rollNo’ = > 44, ’name’ = > ’ Alice’ , ’section’ = > ’B’ ), 2 = > array ( ’rollNo’ = > 3, ’name’ = > ’ Amit’ , ’section’ = > ’ B’ ), 3 = > array ( ’rollNo’ = > 3, ’name’ = > ’ Bob’ , ’section’ = > ’ A’ ), 4 = > array ( ’rollNo’ = > 5, ’name’ = > ’ Gaurav’ , ’section’ = > ’ B’ ), 5 = > array ( ’rollNo’ = > 5, ’name’ = > ’ Gaurav’ , ’section’ = > ’ A’ ) );  
// Define a search list with multiple keys = > value pair $search_items = array ( ’rollNo’ = > 5, ’section’ = > " A " );  
// Call the search and pass the array and
// search list $res = search ( $arr , $search_items );  
// Print search result

foreach ( $res as $var ) { echo ’RollNo:’ . $var [ ’rollNo’ ].
; echo ’Name:’ ​​ . $var [ ’name’ ].
; echo ’Section:’ . $var [ ’section’ ].
; }  
?>
Exit:

 RollNo: 5 
Name: Gaurav
Section: A

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method