Client
From a POST request I get a mixed structure of an array and JSON. To handle multiple type of elements I am using var_dump
to get the passes. For $_POST
I get this:
array(2) { ["json_data"]=> string(677) "[{"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"}]", ["other_data"]=> string(11) "Other_Data" }
Server
In PHP server side I am executing a function that reduce this $_POST
to this array:
array(2) { ["JsonData"]=> array(10) { ["firstname"]=> string(0) "" ["lastname"]=> string(0) "" ["email"]=> string(0) "" ["countryCode"]=> string(0) "" ["phone"]=> string(0) "" ["i_signup_password"]=> string(0) "" ["i_signup_password_rep"]=> string(0) "" ["i_signin_password"]=> string(0) "" ["form"]=> string(11) "d-sys-login" ["process"]=> string(8) "e-signin" } ["otherdata"]=> string(9) "otherdata" }
Code
You can see this runs online.
Can someone help me simplify or improve the script?
- Application of best practices and design pattern usage
- Potential security issues
- Performance
- Correctness in unanticipated cases
The script used to meet this output is this:
<?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);
Examples and Explanation:
Main function is buildVirtualData
This function tries to parse the $_POST
variable; and seeks to reduce it; eliminating the excess of levels in the resulting arrays.
if you check the examples for this variable (arrays plus JSON) 2 arrays + JSON String:
$_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';
the output, Check that this include 2 Main array
:: JsonData
and otherdata
:
array(2) { ["JsonData"]=> array(10) { ["firstname"]=> string(0) "" ["lastname"]=> string(0) "" ["email"]=> string(0) "" ["countryCode"]=> string(0) "" ["phone"]=> string(0) "" ["i_signup_password"]=> string(0) "" ["i_signup_password_rep"]=> string(0) "" ["i_signin_password"]=> string(0) "" ["form"]=> string(11) "d-sys-login" ["process"]=> string(8) "e-signin" } ["otherdata"]=> string(9) "otherdata" }
While for this other variable (Only one array
with JSON
String):
$_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"}]';
the result is one less array level in this case (removing the JsonData
index which is unnecessary in this scope):
array(10) { ["firstname"]=> string(0) "" ["lastname"]=> string(0) "" ["email"]=> string(0) "" ["countryCode"]=> string(0) "" ["phone"]=> string(0) "" ["i_signup_password"]=> string(0) "" ["i_signup_password_rep"]=> string(0) "" ["i_signin_password"]=> string(0) "" ["form"]=> string(11) "d-sys-login" ["process"]=> string(8) "e-signin" }
the other 2 Function used:
valJson
is to validate if value is an JSON String
and can be used as Object
or Array
.
reduArray
is the function that performs a reduction of each Array
.
Why a function that does all this?
I don't have control of the javascript code, I can only offer solutions in php code; What I can do is verify each scenario of what the server receives and this is the slightly more complex script I have.
in fact the JsonData
index is not relevant take care of this: according to the documentation, everything that comes inside JsonData
(the String Json) will be the inputs filled in a form and their value associated, so in reality the string is more important; and that the indexes and string values become indexes
of $_POST
...
for example: there is a form with multiple checkboxes, they can be around 600 in total! Let's say they are to manage the permissions of the process screen and each process screen has 10 possible permission buttons, which are defined according to whether the checkbox is checked or not; There is no way that the server supports sending 600+ inputs in a single request without manipulating the server...
so the ingenious javascript programmer decided to put the 600 input and their filled values (formatted as string) inside a JSON
variable (JsonData
index / array) to be able to pass them to the server. I assure you, I do not share this idea; but need work around this.
These results are correct, but I am looking to improve the code based on the objectives of this community.