$_POST parser - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of $_POST parser.php

  1. <?php
  2.  
  3. #Example Array
  4.  
  5. $_POST = array(
  6.     "valor1" => 1200,
  7.     "valor2" => "texto",
  8.     "valor3" => true,
  9.     "valor4" => '{"seclvl_text":"datp","seclvl_boolean":"false"}',
  10. );
  11.  
  12. function ValidateJson($Var) {
  13.     if (!is_array($Var)) {
  14.         return ((json_decode($Var) != null) AND (is_object(json_decode($Var)) OR is_array(json_decode($Var)))) ? true : false;
  15.     } else {
  16.         return false;
  17.     }
  18. }
  19.  
  20. function buildVirtualData($data) {
  21.     if (is_array($data)) {
  22.         $result = [];
  23.         foreach ($data as $key1 => $val1) {
  24.             $valJson = ValidateJson($val1);
  25.             if ($valJson) {
  26.                 $jsonObj       = json_decode($val1, true);
  27.                 $result[$key1] = buildVirtualData($jsonObj);
  28.             } elseif ($valJson == false && is_array($val1)) {
  29.                 foreach ($val1 as $key2 => $val2) {
  30.                     $result[$key1][$key2] = buildVirtualData($val2);
  31.                 }
  32.             } else {
  33.                 if ($val1 === 'true') {
  34.                     $val1 = true;
  35.                 } else if ($val1 === 'false') {
  36.                     $val1 = false;
  37.                 }
  38.                 $result[$key1] = $val1;
  39.             }
  40.         }
  41.         return $result;
  42.     } else {
  43.         if (ValidateJson($data)) {
  44.             $jsonObj = json_decode($data, true);
  45.             return buildVirtualData($jsonObj);
  46.         } else {
  47.             return $data;
  48.         }
  49.     }
  50. }
  51.  
  52. $data = buildVirtualData($_POST);
  53. echo '<pre>';
  54. echo var_dump($data);
  55. echo '</pre>';
File Description
  • $_POST parser
  • PHP Code
  • 29 Oct-2019
  • 1.52 Kb
You can Share it: