Find value in a multidimensional PHP array | 2023 Code Examples
Find a value in multidimensional PHP arrays code example function recursiveFunc($arr,$player_name = null) { foreach($arr as $key=>$value) { if($key == "Name") { $player_name = $value; } if(gettype($value) == "array") { //If array within array then recall the function. recursiveFunc($value,$player_name); } if($value == "Quarantine2k19") { echo "Player: ".$player_name." took part in the Quarantine2k19 tournament"; } } } //Call the function. recursiveFunc($players_arr); //OUTPUT //Player: Bond007 took part in the Quarantine2k19 tournament We have done articles related to array filtering by key and value. This article takes a step further and discovers various approaches to find value in multidimensional array PHP. A multidimensional array could be any level deeper, and that is often a challenge when we have to find a value that resides deep within an array. There are two approaches to go about this problem: Iterative and Recursive. The iterative approach uses nested loops to iterate over the levels of arrays…