3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data='{ "name": "josh", "type": "human", "friends": [ { "name": "ben", "type": "robot" }, { "name": "tom", "type": "alien" } ], "img": "img/path" }'; $friend_name = "ben"; $json=json_decode($data); $friends=$json->friends; foreach( $friends as $val){ if($friend_name == $val->name) { echo "name = ".$val->name; echo "type = ".$val->type; } }
Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.10, 7.1.0, 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.3 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
name = bentype = robot

preferences:
101.05 ms | 404 KiB | 179 Q