Change language

How to push a value based on a mapped value in PHP?

Approach:create two different associative arrays named array1and array2 . Then compare the value of array1 with the key of array2, and if we get a match, we insert a static key and a pair of values ​​like this:Program 1:

 
// Create an associative array $array1 = [ "GFG1" = > "1" , "GFG2" = > "2" , "GFG3" = > "3"];  
// Create multidimensional
// associative array $array2 = [ "1" = > [ "Subject" = > "HTML" ], "2" = > [ "Subject" = > "CSS" ], "3" = > [ "Subject" = > "JavaScript" ] ];  
// Nested foreach for comparison // array1 and array2 elements foreach ( $array1 as $key1 = > $value1 ) { foreach ( $array2 as $key2 = > $value2 ) { // Compare array 2 key with // array1 value if ( $key2 == $value1 ) { // If a match is found, click // element based on array 2 // on the key $array2 [ $value1 ] [ "Organization" ] = "GeeksforGeeks" ; } } }  
// Show array elements print_r ( $array2 );  
?>
Exit:
Array ([ 1] = > Array ([Subject] = > HTML [Organization] = > GeeksforGeeks) [2] = > Array ([Subject] = > CSS [Organization] = > GeeksforGeeks) [3] = > Array ([Subject] = > JavaScript [Organization] = > GeeksforGeeks)) 
The above program places a value into an existing array. If you want to put the value into an entire new array, see the program below. 
Program 2:  
// Create an associative array $array1 = [ "GFG1" = > "1" , "GFG2" = > "2" , "GFG3" = > "3"];  
// Create multidimensional
// associative array $array2 = [ "1" = > [ "Subject" = > "HTML" ], "2" = > [ "Subject" = > "CSS" ], "3" = > [ "Subject" = > "JavaScript" ] ];  
// Create empty array $array3 = [];  
// Nested foreach for comparison // array1 and array2 elements foreach ( $array1 as $key1 = > $value1 ) { foreach ( $array2 as $key2 = > $value2 ) { // Compare array 2 key with // array1 value if ( $key2 == $value1 ) { // If a match is found, click // array element in array3 // based on key $array3 [ $value1 ] = $array2 [ $value1 ]; } } }  
// Show array elements print_r ( $array3 );  
?>
Exit:
Array ([ 1] = > Array ([Subject] = > HTML) [2] = > Array ([Subject] = > CSS) [3] = > Array ([Subject] = > JavaScript))  The above program will send the values ​​from array 2 to array 3, where array 3 is empty.Program 3:         
// Create associative array $department = [ "dept1" = > "IT" , "dept2" = > "CE" , "dept3" = > "CS" , "dept4" = > "EC"];  
// Create multidimensional associative array $student = [ "IT" = > [ "total_students" = > "60" ], "CE" = > [ "total_students" = > "65" ], "CS" = > [ "total_students" = > "62" ] ];  
// Nested foreach for comparison // $department and $student elements foreach ( $department as $dept = > $d ) { foreach ( $student as $std = > $s ) { // Compare the $student array key with // $department array value
if ( $std == $d ) { $student [ $d ] [ " HOD " ] = " XYZ " ; } } }  
// Show array elements print_r ( $student );  
?>
Exit:
Array ([ IT] = > Array ([total_students] = > 60 [HOD] = > XYZ) [CE] = > Array ([total_students] = > 65 [HOD] = > XYZ) [CS] = > Array ([total_students] = > 62 [HOD] = > XYZ)) 
In the above program there is no match for dept4 in the students array, so it does not appear in the output section.

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