$_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.
Result of php executing
Full code of $_POST parser example.php
- <?php
- #Example Array
- $_POST = array(
- "valor1" => 1200,
- "valor2" => "texto",
- "valor3" => true,
- "valor4" => '{"seclvl_text":"datp","seclvl_boolean":"false"}',
- );
- #decodeand validate
- function jsonDecodeAndValidate($var) {
- if(!is_string($var))
- return $var;
- if(!$decoded = json_decode($var, true))
- return $var;
- if(!is_array($decoded))
- return $var;
- return $decoded;
- }
- #Parse Function
- function buildVirtualData($data, &$build) {
- $data = jsonDecodeAndValidate($data);
- if(is_array($data) || is_object($data)){
- foreach($data as $key => $value){
- buildVirtualData($value, $build[$key]);
- }
- } else {
- if(in_array($data, ['true', 'false'])){
- $data = filter_var($data, FILTER_VALIDATE_BOOLEAN);
- }
- $build = $data;
- }
- }
- # call to Function:
- $build = [];
- buildVirtualData($_POST, $build);
- echo '<pre>';
- echo json_encode($build, JSON_PRETTY_PRINT);
- echo '</pre>';