3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = json_decode('{ "name": "", "type": "layout", "children": [ { "name": "", "type": "section", "children": [ { "name": "", "type": "row", "children": [ { "name": "", "type": "column", "props": { }, "children": [] }, { "type": "column", "children": [ { "type": "itemdata", "props": { "basevalue": "100" }, "children": [] } ] } ] } ] }]}',true); //print_r($arr); function getBaseValue($arr,$search_key){ $pending_calls = []; foreach($arr as $key => $value){ if(is_array($value)){ $pending_calls[] = $value; }else if($search_key === $key){ return $value; } } foreach($pending_calls as $call){ $returned_val = getBaseValue($call,$search_key); if($returned_val !== false) return $returned_val; } return false; } echo getBaseValue($arr,'basevalue');
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
100

preferences:
153.74 ms | 407 KiB | 5 Q