Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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);
  
?>


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:




<?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);
  
?>


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:




<?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);
  
?>


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.



Last Updated : 10 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads