3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $json = '[ { "vartype":"value", "varid":"device_name", "varvalue":"Speedport Hybrid" }, { "vartype":"value", "varid":"provis_inet", "varvalue":"x03" }, { "vartype":"value", "varid":"provis_voip", "varvalue":"xx3" }, { "vartype":"value", "varid":"ppp_bnguser", "varvalue":"0" }, { "vartype":"value", "varid":"bngscrat", "varvalue":"0" }, { "vartype":"value", "varid":"router_state", "varvalue":"OK" }, { "vartype":"value", "varid":"support_https", "varvalue":"0" }, { "vartype":"page_title", "varid":"title", "varvalue":"Speedport Hybrid Konfigurationsprogramm" }, { "vartype":"status", "varid":"onlinestatus", "varvalue":"online" }, { "vartype":"option", "varid":"use_lte", "varvalue":"1" }, { "vartype":"value", "varid":"lte_status", "varvalue":"10" }, { "vartype":"value", "varid":"bonding_status", "varvalue":"Online" }, { "vartype":"value", "varid":"lte_signal", "varvalue":"4" }, { "vartype":"status", "varid":"loginstate", "varvalue":"0" }, { "vartype":"value", "varid":"datetime", "varvalue":"11.09.2015 06:34:56" }, { "vartype":"value", "varid":"device_name", "varvalue":"Speedport Hybrid" }, { "vartype":"value", "varid" :"imei", "varvalue":"864230020436034" }, { "vartype":"value", "varid":"dsl_link_status", "varvalue":"online" }, { "vartype":"value", "varid":"dsl_errnr", "varvalue":"" }, { "vartype":"value", "varid":"status", "varvalue":"online" }, { "vartype":"value", "varid":"fail_reason", "varvalue":"" }, { "vartype":"value", "varid":"inet_errnr", "varvalue":"" }, { "vartype":"value", "varid" :"connect", "varvalue":"0" }, { "vartype":"value", "varid":"dsl_downstream", "varvalue":"2299" }, { "vartype":"value", "varid":"dsl_upstream", "varvalue":"543" }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"1" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*074" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"2" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*208" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"3" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*365" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"4" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*151" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"5" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*169" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"6" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*803" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"7" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*156" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"8" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*121" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"addphonenumber", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"9" }, { "vartype":"value", "varid":"phone_number", "varvalue":"*160" }, { "vartype":"value", "varid":"failreason", "varvalue":"0" }, { "vartype":"value", "varid":"status", "varvalue":"ok" }, { "vartype":"value", "varid":"voip_errnr", "varvalue":"" } ] }, { "vartype":"template", "varid":"adddect", "varvalue":[ { "vartype":"value", "varid":"id", "varvalue":"1" } ] }, { "vartype":"value", "varid":"use_dect", "varvalue":"1" }, { "vartype":"value", "varid":"wlan_ssid", "varvalue":"WLAN-SVEJAKI" }, { "vartype":"value", "varid":"wlan_5ghz_ssid", "varvalue":"WLAN-SVEJAKI" }, { "vartype":"value", "varid":"use_wlan", "varvalue":"1" }, { "vartype":"value", "varid":"use_wlan_5ghz", "varvalue":"0" }, { "vartype":"value", "varid":"wlan_devices", "varvalue":"2" }, { "vartype":"value", "varid":"wlan_5ghz_devices", "varvalue":"0" }, { "vartype":"value", "varid":"lan1_device", "varvalue":"1" }, { "vartype":"value", "varid":"lan2_device", "varvalue":"1" }, { "vartype":"value", "varid":"lan3_device", "varvalue":"0" }, { "vartype":"value", "varid":"lan4_device", "varvalue":"0" }, { "vartype":"value", "varid":"use_wps", "varvalue":"1" }, { "vartype":"value", "varid":"hsfon_status", "varvalue":"0" }, { "vartype":"value", "varid":"firmware_version", "varvalue":"050124.02.00.010" }, { "vartype":"value", "varid":"serial_number", "varvalue":"HE33725635" }] '; function getValues($array) { $data = array(); foreach ($array as $item) { if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue; // thank you telekom for this piece of shit if ($item['vartype'] == 'template') { if (is_array($item['varvalue'])) { $data[$item['varid']][] = getValues($item['varvalue']); } else { // i dont know if we need this $data[$item['varid']] = $item['varvalue']; } } else { if (is_array($item['varvalue'])) { $data[$item['varid']] = getValues($item['varvalue']); } else { $data[$item['varid']] = $item['varvalue']; } } } return $data; } print_r(getValues(json_decode($json)));
based on EqXK7
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/ISU8D:514 Stack trace: #0 /in/ISU8D(539): getValues(Array) #1 {main} thrown in /in/ISU8D on line 514
Process exited with code 255.
Output for 5.3.17 - 5.6.28
Fatal error: Cannot use object of type stdClass as array in /in/ISU8D on line 514
Process exited with code 255.