请求与 JSON 字符串和数组字段混合,减少自定义函数中丢失数据的情况

从POST请求中我得到和混合结构JSON和字段Array;处理多种类型的元素;我已经执行和 var_dump 来获取通行证$_POST并得到这个:


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"

}

在 PHP 服务器端,我正在执行一个函数,将其减少$_POST为这个数组:

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"

}

主要问题是最后一个成员数组丢失:

  ["other_data"]=> 
   string(11) "other_data"
  1. 我怎样才能防止丢失数据?

  2. 这个脚本可以更简单吗?


慕娘9325324
浏览 59回答 1
1回答

慕斯王

解决方案脚本,但需要使其更简单:<?php&nbsp; &nbsp; function buildVirtualData($data)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($data)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp[$key] = buildVirtualData($value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return reduArray($temp);&nbsp; &nbsp; &nbsp; &nbsp; } elseif (valJson($data)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json_obj = json_decode($data, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($json_obj as $key1 => $json_sub_obj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($json_sub_obj as $key2 => $value2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_array($value2)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($value2 as $keyof => $valueof) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp[$keyof] = buildVirtualData($valueof);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json_obj[$key1][$key2] = $temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ('true' === $value2 || true === $value2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json_obj[$key1][$key2] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } elseif ('false' === $value2 || false === $value2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json_obj[$key1][$key2] = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json_obj[$key1][$key2] = $value2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return reduArray($json_obj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ('true' === $data || true === $data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } elseif ('false' === $data || false === $data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $data;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function valJson($var)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!is_array($var)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ((json_decode($var) != null) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (is_object(json_decode($var)) || is_array(json_decode($var)))) ? true : false;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; function reduArray($array)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $result = $array;&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($array)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $check = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($array as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!is_array($value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $check = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($check) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = array_reduce($array, 'array_merge', []);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $_POST=[];&nbsp; &nbsp; $_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"}]';&nbsp; &nbsp; $_POST['otherdata']='otherdata';&nbsp; &nbsp; $_POST=buildVirtualData($_POST);&nbsp; &nbsp; $_POST=reduArray($_POST);&nbsp; &nbsp; echo var_dump($_POST);
打开App,查看更多内容
随时随地看视频慕课网APP