3v4l.org

run code in 300+ PHP versions simultaneously
<?php // take the badly formatted JSON $result = '{ "price": [ { "price": "2000", "origin_name": "JPN", }, { "price": "5000", "origin_name": "USA", } ] }'; // and remove the superfluous commas: $pat = "/(o.+),/"; $replace = "$1"; $nu_result = preg_replace($pat,$replace,$result); // now convert JSON into PHP array and traverse it $resultDecoded = json_decode($nu_result, true); $arr = array_pop($resultDecoded); array_walk_recursive($arr,function($e,$i) { if ($i == "price") { echo "price: ",$e,"\n"; } else if ($i == "origin_name") { echo "origin_name: ",$e,"\n\n"; } });
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 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.27, 8.4.1 - 8.4.14
price: 2000 origin_name: JPN price: 5000 origin_name: USA

preferences:
174.88 ms | 408 KiB | 5 Q