1 回答
TA贡献1864条经验 获得超2个赞
解决方案脚本,但需要使其更简单:
<?php
function buildVirtualData($data)
{
if (is_array($data)) {
$temp = [];
foreach ($data as $key => $value) {
$temp[$key] = buildVirtualData($value);
}
return reduArray($temp);
} elseif (valJson($data)) {
$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 ('true' === $data || true === $data) {
$data = true;
} elseif ('false' === $data || false === $data) {
$data = false;
}
return $data;
}
}
function valJson($var)
{
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)
{
$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;
}
$_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';
$_POST=buildVirtualData($_POST);
$_POST=reduArray($_POST);
echo var_dump($_POST);
- 1 回答
- 0 关注
- 58 浏览
添加回答
举报