Update specific key in multidimensional array in PHP
This article demonstrates how to recursively update specific key in multidimensional array in PHP.
Introduction
Multidimensional arrays add alot to the complexity than a linear array. Similarly, updating specific keys in a multidimensional array using an iterative approach is complex and unpredictable.
So, in this article, we take up the challenge and explore how to update specific key in multidimensional array in PHP.
How to update key value in array PHP?
The article “How to update key value in array PHP” explores three options for changing keys in a linear array.
- A loop approach.
- The direct indexing approach.
- Using PHP array_search function.
Out of these three, direct indexing stands out in terms of efficiency. That said, this article will use a mix of these to develop a generic function to handle multidimensional arrays.
How to update specific key in multidimensional array in PHP
Before diving into code examples, let’s first see the multidimensional array.
$students =
[
"Sam" => ["Exam#1" => 87],
"Brian" => ["Exam#1" => 80],
"Chris" => ["Exam#1" => 92],
"Sarah" => ["Exam#1"=> 90]
];
The array $students
is a two-dimensional array. The objectives here are to
- Find the key in multidimensional array.
- Update specific key in multidimensional array.
Let’s see an example that uses a loop and direct indexing to update the key “Exam#1” to “MidTerm” in all the subarrays.
<?php
$students =
[
"Sam" => ["Exam#1" => 87],
"Brian" => ["Exam#1" => 80],
"Chris" => ["Exam#1" => 92],
"Sarah" => ["Exam#1"=> 90]
];
function changeName($array, $oldKey, $newKey)
{
foreach($array as $k => &$v)
{
if(array_key_exists($oldKey, $v)) //If key exists in the subarray
{
$value = $v[$oldKey]; //Passes the value to a variable using direct indexing.
$v[$newKey] = $value; //Make a pair with the new key.
unset($v[$oldKey]); //Unsets the old key and value pair.
}
}
return $array;
}
$students = changeName($students, 'Exam#1', 'MidTerm');
?>
Here’s the breakdown of the function changeName
.
- The loop iterates through the array and
$v
refers to the subarrays. - PHP array_key_exists checks if the key is available a subarray.
- The block within array_key_exists updates the key and unsets the older one.
- Finally, the function returns the updated array.
So, here’s the output of the function for the array above.
Array
(
[Sam] => Array
(
[MidTerm] => 87
)
[Brian] => Array
(
[MidTerm] => 80
)
[Chris] => Array
(
[MidTerm] => 92
)
[Sarah] => Array
(
[MidTerm] => 90
)
)
Looks cool! But let’s assume the multidimensional array changes to something as follows.
$students =
[
[
"Name" => "Sam",
"Exams" => [
"Exam#1" => 87
]
],
[
"Name" => "Brian",
"Exams" => [
"Exam#1" => 80
],
[
"Name" => "Chris",
"Exams" => [
"Exam#1" => 92
]
],
[
"Name" => "Sarah",
"Exams" => [
"Exam#1" => 90
]
],
],
];
It seems gibberish but worse than that is the function doesn’t support this structure. It will instantly throw an error. In simpler terms, it is not generic enough. So, what’s the way out then?
The way out is writing a generic function using recursion. So, up next we’ll see how to update specific key in multidimensional array using recursion.
Recursively update specific key in multidimensional array in PHP
Recursive algorithms adapt to arrays regardless of their dimensions and that’s why they are super flexible and robust. The following example uses a recursive function to update specific key in multidimensional array in PHP.
function changeName(&$array, $oldKey, $newKey)
{
foreach($array as $k => &$v)
{
if(gettype($v) == 'array')
{
if(array_key_exists($oldKey, $v)) //If key exists in the subarray
{
$value = $v[$oldKey]; //Passes the value to a variable using direct indexing.
$v[$newKey] = $value; //Make a pair with the new key.
unset($v[$oldKey]); //Unsets the old key and value pair.
}
else
{
changeName($v, $oldKey, $newKey); //Go a level deep to find the key.
}
}
}
}
The changeName function has been modified into a recursive function. The core logic of updating the key looks the same. The recursive calls happen if the function doesn’t find the key in a subarray.
So, it will go as many levels down as possible just to find and update a specific key. Also, note that we are not returning from the function because it takes the array by reference thus making in place changes.
Here’s the output.
Array
(
[0] => Array
(
[Name] => Sam
[Exams] => Array
(
[MidTerm] => 87
)
)
[1] => Array
(
[Name] => Brian
[Exams] => Array
(
[MidTerm] => 80
)
[0] => Array
(
[Name] => Chris
[Exams] => Array
(
[MidTerm] => 92
)
)
[1] => Array
(
[Name] => Sarah
[Exams] => Array
(
[MidTerm] => 90
)
)
)
)
Voila! We’ve made a generic function to update specific key in a multidimensional array in PHP.
Conclusion
The article overviews the challenges in working with a multidimensional array and then explores an example that is non-generic but successfully updates a specific key. Then, the article moves to a generic recursive function that updates a specific key in a multidimensional array of any depth.
Hopefully, you’ve liked this article. Learn more about PHP at FuelingPHP.
Want to learn more about PHP?
We have many fun articles related to PHP. You can explore these to learn more about PHP.