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.