3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '{ "world": { "france": { "city": { "city_1": { "name": "paris", "titre": "lorem ipsum" }, "city_2": { "name": "marseille", "titre": "dolor sit amet" } } }, "usa": { "city": { "city_1": { "name": "new york", "titre": "lorem ipsum" }, "city_2": { "name": "los angeles", "titre": "lorem ipsum" }, "city_3": { "name": "portland", "titre": "lorem ipsum" } } } } }'; $data = json_decode($json, true); $scenario = ''; foreach($data['world'] as $country=>$cities) { $cityArray = array(); foreach($cities['city'] as $key=>$values) { $cityArray[] = $values['name']; } $scenario .= '<option value="'.$country.'" data-city="'.implode(',', $cityArray).'">'.$country.'</option>'; } echo $scenario;
Output for 5.5.1, 7.3.0 - 7.3.33, 7.4.0 - 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.4, 8.3.6 - 8.3.7
<option value="france" data-city="paris,marseille">france</option><option value="usa" data-city="new york,los angeles,portland">usa</option>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <option value="france" data-city="paris,marseille">france</option><option value="usa" data-city="new york,los angeles,portland">usa</option>

preferences:
151.02 ms | 402 KiB | 159 Q