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

In PHP, to push the value in an array on match we need an array with key and value pair. An array which contains key and value pair is known as associative array.

Approach: Create two different associative array named as array1 and array2. Then compare the value of array1 with the key of array2 and if we get a match then we will push a static key and value pair as follows:

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Creating an associative array
$array1 = [
    "GFG1" => "1",
    "GFG2" => "2",
    "GFG3" => "3"
];
  
// Creating a multi-dimensional
// associative array
$array2 = [
    "1" => ["Subject" => "HTML"],
    "2" => ["Subject" => "CSS"],
    "3" => ["Subject" => "JavaScript"]
];
  
// Nested foreach loop to compare
// array1 and array2 elements
foreach( $array1 as $key1 => $value1 ) {
      
    foreach( $array2 as $key2 => $value2 ) {
          
        // Compare the key of array2 with
        // the value of array1
        if ($key2 == $value1) {
              
            // If a match is found then push
            // the element in array2 based
            // on the key
            $array2[$value1]["Organization"]
                        = "GeeksforGeeks";
        }
    }
}
  
// Display the array elements
print_r($array2);
  
?>

chevron_right


Output:

Array
(
    [1] => Array
        (
            [Subject] => HTML
            [Organization] => GeeksforGeeks
        )

    [2] => Array
        (
            [Subject] => CSS
            [Organization] => GeeksforGeeks
        )

    [3] => Array
        (
            [Subject] => JavaScript
            [Organization] => GeeksforGeeks
        )

)

In the above program, the value is pushed in an existing array. If you want to push value in a whole new array then see the below program.
Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Creating an associative array
$array1 = [
    "GFG1" => "1",
    "GFG2" => "2",
    "GFG3" => "3"
];
  
// Creating a multi-dimensional
// associative array
$array2 = [
    "1" => ["Subject" => "HTML"],
    "2" => ["Subject" => "CSS"],
    "3" => ["Subject" => "JavaScript"]
];
  
// Create an empty array
$array3 = [];
  
// Nested foreach loop to compare
// array1 and array2 elements
foreach( $array1 as $key1 => $value1 ) {
      
    foreach( $array2 as $key2 => $value2 ) {
          
        // Compare the key of array2 with
        // the value of array1
        if ($key2 == $value1) {
              
            // If a match is found then push
            // the array element into array3
            // based on the key
            $array3[$value1] = $array2[$value1];
        }
    }
}
  
// Display the array elements
print_r($array3);
  
?>

chevron_right


Output:

Array
(
    [1] => Array
        (
            [Subject] => HTML
        )

    [2] => Array
        (
            [Subject] => CSS
        )

    [3] => Array
        (
            [Subject] => JavaScript
        )

)

The above program will push the values from the array2 to array3 where the array3 is empty.

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create an associative array
$department = [
    "dept1" => "IT",
    "dept2" => "CE",
    "dept3" => "CS",
    "dept4" => "EC"
];
  
// Create a multi-dimensional associative array
$student = [
    "IT" => ["total_students" => "60"],
    "CE" => ["total_students" => "65"],
    "CS" => ["total_students" => "62"]
];
  
// Nested foreach loop to compare
// $department and $student elements
foreach ($department as $dept => $d) {
    foreach ($student as $std => $s) {
          
        // Compare the key of $student array with
        // the value of $department array
        if ($std == $d) {
            $student[$d]["HOD"] = "XYZ";
        }
    }
}
  
// Display the array elements
print_r($student);
  
?>

chevron_right


Output:

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 the dept4 in the student array so that it is not displayed in the output section.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.