How to push a value based on matched value in PHP ?
Last Updated :
12 Jul, 2025
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
<?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
<?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
<?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.
Similar Reads
How to Push Both Key and Value into PHP Array ? Given an array, the task is to push a new key and value pair into the array. There are some methods to push value and key into the PHP array, these are:Table of ContentUsing Square Bracket [] SyntaxUsing array_merge() FunctionUsing += OperatorUsing the array_combine FunctionUsing array_replace()Usin
4 min read
How to Assign Multiple Variables in One Line in PHP ? In PHP, assigning multiple variables in one line can be a handy and efficient way to streamline your code. This technique not only makes your code more concise but also improves readability. There are several approaches to achieve this, each with its own syntax and use cases. Table of Content Using
2 min read
How to search by key=>value in a multidimensional array in PHP ? In PHP, multidimensional array search refers to searching a key=>value in a multilevel nested array. This search can be done either by the iterative or recursive approach. Table of ContentRecursive ApproachIterative ApproachUsing array_filter() FunctionRecursive Approach:Check if the key exists i
4 min read
How to merge the duplicate value in multidimensional array in PHP? To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements. If duplicity is found then first merge the duplicate el
4 min read
How to Find Matching Pair from an Array in PHP ? Given an Array, the task is to find the matching pair from a given array in PHP. Matching a pair from an array involves searching for two elements that satisfy a certain condition, such as having the same value, summing up to a specific target, or meeting some other criteria. Here, we will cover thr
5 min read
How to get parameters from a URL string in PHP? The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions.Note: The page URL and the parameters are separated by the ? character.parse_url() FunctionThe parse_url() function is used to return the components of a URL by parsing it. It parses a URL and return
2 min read