$_POST parser example - 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 example.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. #decodeand validate
  13. function jsonDecodeAndValidate($var) {
  14.     if(!is_string($var))
  15.         return $var;
  16.     if(!$decoded = json_decode($var, true))
  17.         return $var;
  18.     if(!is_array($decoded))
  19.         return $var;
  20.     return $decoded;
  21. }
  22. #Parse Function
  23. function buildVirtualData($data, &$build) {
  24.     $data = jsonDecodeAndValidate($data);
  25.     if(is_array($data) || is_object($data)){
  26.         foreach($data as $key => $value){
  27.             buildVirtualData($value, $build[$key]);
  28.         }
  29.     } else {
  30.         if(in_array($data, ['true', 'false'])){
  31.             $data = filter_var($data, FILTER_VALIDATE_BOOLEAN);
  32.         }
  33.         $build = $data;
  34.     }
  35. }
  36. # call to Function:
  37. $build = [];
  38. buildVirtualData($_POST, $build);
  39. echo '<pre>';
  40. echo json_encode($build, JSON_PRETTY_PRINT);
  41. echo '</pre>';
File Description
  • $_POST parser example
  • PHP Code
  • 30 Oct-2019
  • 979 Bytes
You can Share it: