All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Replace Value In Multidimensional Array

Last Updated : Mar 11, 2024

PHP Replace Value In Multidimensional Array

In this tutorial we will show you the solution of PHP replace value in multidimensional array, here we needs to use ‘is_array(), array_search()’ methods for replace values in multidimensional array.

We defined array with nested array it contains two arrays there we going to changing one array values with another key name or we can say array because which is nested array name then at last we printed updated result on webpage.

Step By Step Guide On PHP Replace Value In Multidimensional Array :-

Here we defined method ‘chKey()’ with three parameters of array it contains ‘original array- $arr,keys name array- $o_key, key name changed array- $ch_key’.

There we checking whether $arr is array or not for confirmation and defined empty array variable ‘$temp_arr’ for store result of updated array.

Using foreach loop we iterating array keys one by one then within that using array_search() method we searching where key name of array ‘$o_key’ match with $ch_key[$key’s of $o_key] if they matched then we storing that key to variable ‘$key’.

Then we checking found key’s respective values whether array or not if they array then we passing that value ‘$value’ array to chKey() method for does same iterate and If they not array then we storing them with updated key ‘$key’ of array ‘$temp_arr’ and we returns at last we printed result on webpage.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj));
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined nested array ‘$info’ there we defined two arrays ‘movie,color’ with respective set of array values ‘array("Thor", "IronMan", "Frozen"), array("Blue", "Red", "SkyBlue")’.
  4. Defined variables of ‘$o_key, $ch_key’ with respective array values ‘array("movie" , "color"), array("Movies" , "color")’ for pass them as parameter to method chKey() method.
  5. Here array ‘$o_key’ contains nested array ‘$info’ key names as array values, ‘$ch_key’ contains updated key name as array values.
  6. We defined function call chKey() with above seen array variables ‘$info,$o_key,$ch_key’ as parameters then returning results will stored on variable $res and last we printed result on webpage using print_r() method.
  7. In function chKey() we checking whether ‘$arr’ is array or not by ‘is_array()’ method if they contain value not array means we retuning them otherwise we pass that to next process.
  8. Then we defined empty array variable ‘$temp_arr’ for store updated result. Using foreach loop we iterating array $arr values one by one.
  9. Within loop using array_search() method with parameters ‘$key,$o_key’ for checking with $ch_key array values to compare them type of value whether matching or not then result key updated keys stored on variable ‘$key’.
  10. Using is_array() method we checking $value is array or not if that is not array then we storing those $value array to $temp_arr array with ‘$key’. If $value is array then we again passed to chKey() method for iteration and then finally we returns.

Conclusion :-

In conclusion now we are able to know how to replace values in multidimensional array using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will prints result of updated array ‘Array ( [Movies] => Array ( [0] => Thor [1] => IronMan [2] => Frozen ) [color] => Array ( [0] => Blue [1] => Red [2] => SkyBlue ) )’.

We can also change that defined array ‘$arr’ data with some other values and needs to modify which is going to replacing values as per your changes on array ‘$arr’ then we gets result without any error.

I hope this tutorial on PHP replace value in multidimensional array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪