3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildVirtualData($data) { if (is_array($data)) { //check if is an array Walk trough to rebuild $temp = []; foreach ($data as $key => $value) { $temp[$key] = buildVirtualData($value); } return reduArray($temp); } elseif (valJson($data)) { //check if is an JSON, Walk through to rebuild as an array $json_obj = json_decode($data, true); foreach ($json_obj as $key1 => $json_sub_obj) { foreach ($json_sub_obj as $key2 => $value2) { if (is_array($value2)) { $temp = []; foreach ($value2 as $keyof => $valueof) { $temp[$keyof] = buildVirtualData($valueof); } $json_obj[$key1][$key2] = $temp; } else { if ('true' === $value2 || true === $value2) { $json_obj[$key1][$key2] = true; } elseif ('false' === $value2 || false === $value2) { $json_obj[$key1][$key2] = false; } else { $json_obj[$key1][$key2] = $value2; } } } return reduArray($json_obj); } } else { // if it is not an array or a JSON; evaluate the type if it is text and meets possible boolean values if ('true' === $data || true === $data) { $data = true; } elseif ('false' === $data || false === $data) { $data = false; } return $data; } } function valJson($var) //JSON Validator { if (!is_array($var)) { return ((json_decode($var) != null) && (is_object(json_decode($var)) || is_array(json_decode($var)))) ? true : false; } else { return false; } } function reduArray($array) //array Reductor { $result = $array; if (is_array($array)) { $check = true; foreach ($array as $key => $value) { if (!is_array($value)) { $check = false; break; } } if ($check) { $result = array_reduce($array, 'array_merge', []); } } return $result; } //Example Data //$_POST=[]; //$_POST['JsonData']='[{"firstname":""},{"lastname":""},{"email":""},{"countryCode":""},{"phone":""},{"i_signup_password":""},{"i_signup_password_rep":""},{"email":""},{"i_signin_password":""},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"}]'; //$_POST['otherdata']='otherdata'; //Execution of Function hover $_POST Variable. //$_POST=buildVirtualData($_POST); //$_POST=reduArray($_POST); //echo var_dump($_POST); $walter = reduArray( buildVirtualData( $_POST = [ 'JsonData' => '[{"firstname":""},{"lastname":""},{"email":""},{"countryCode":""},{"phone":""},{"i_signup_password":""},{"i_signup_password_rep":""},{"email":""},{"i_signin_password":""},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"},{"form":"d-sys-login"},{"process":"e-signin"}]' ] ) ); $mick = []; foreach ($_POST as $key => $value) { if ($key !== 'JsonData') { $mick[$key] = $value; } else { $mick = array_merge($mick, ...json_decode($value, true)); } } var_export($walter); echo "\n---\n"; var_export($mick);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
array ( 'firstname' => '', 'lastname' => '', 'email' => '', 'countryCode' => '', 'phone' => '', 'i_signup_password' => '', 'i_signup_password_rep' => '', 'i_signin_password' => '', 'form' => 'd-sys-login', 'process' => 'e-signin', ) --- array ( 'firstname' => '', 'lastname' => '', 'email' => '', 'countryCode' => '', 'phone' => '', 'i_signup_password' => '', 'i_signup_password_rep' => '', 'i_signin_password' => '', 'form' => 'd-sys-login', 'process' => 'e-signin', )

preferences:
37.41 ms | 461 KiB | 5 Q