POST variable analysis contains JSON format string PHP I am verifying certain codes that work well and I don't know what else to optimize in them or what other tests to do: in this case it is a php script that takes the data of the $_POST variable analyzes them and converts them into an array, this is since in some cases the post field may contain a JSon string.

I leave a functional example where I have manually set the $_POST variable to do the Demonstration:

https://wtools.io/php-sandbox/bqsT

Original Code example:

&lt;?php

#Example Array

$_POST = array(
    "valor1" =&gt; 1200,
    "valor2" =&gt; "texto",
    "valor3" =&gt; true,
    "valor4" =&gt; '{"seclvl_text":"datp","seclvl_boolean":"false"}',
);
#Validate Function for Json
function ValidateJson($Var) {
    if (!is_array($Var)) {
        return ((json_decode($Var) != null) AND (is_object(json_decode($Var)) OR is_array(json_decode($Var)))) ? true : false;
    } else {
        return false;
    }
}
#Parse Function
function buildVirtualData($data) {
    if (is_array($data)) {
        $result = [];
        foreach ($data as $key1 =&gt; $val1) {
            $valJson = ValidateJson($val1);
            if ($valJson) {
                $jsonObj       = json_decode($val1, true);
                $result[$key1] = buildVirtualData($jsonObj);
            } elseif ($valJson == false &amp;&amp; is_array($val1)) {
                foreach ($val1 as $key2 =&gt; $val2) {
                    $result[$key1][$key2] = buildVirtualData($val2);
                }
            } else {
                if ($val1 === 'true') {
                    $val1 = true;
                } else if ($val1 === 'false') {
                    $val1 = false;
                }
                $result[$key1] = $val1;
            }
        }
        return $result;
    } else {
        if (ValidateJson($data)) {
            $jsonObj = json_decode($data, true);
            return buildVirtualData($jsonObj);
        } else {
            return $data;
        }
    }
}
# call to Function:
$data = buildVirtualData($_POST);
echo '&lt;pre&gt;';
echo var_dump($data);
echo '&lt;/pre&gt;';


this parse have the feature that convert any text true or false to Boolean.
I think you're are definitely on the right track here. I spent a good deal of time trying to refactor, here's what I came up with 

#Validate Function for Json
function jsonDecodeAndValidate($var, $assoc = false) {
    if(!is_string($var))
        return false;
    if(!$decoded = json_decode($var, $assoc))
        return false;
    if(!is_object($decoded) &amp;&amp; !is_array($decoded))
        return false;
    return $decoded;
}
#Parse Function
function buildVirtualData($data) {
    if (is_array($data) || is_object($data)) {
        foreach ($data as $key1 =&gt; $val1) {

            if(is_array($val1)){
                foreach ($val1 as $key2 =&gt; $val2) {
                    $result[$key1][$key2] = buildVirtualData($val2);
                }
            }
            else if($decoded = jsonDecodeAndValidate($val1)){
                $result[$key1] = buildVirtualData($decoded);
            } else {
                if(in_array($val1, ['true', 'false']))
                    $val1 = filter_var($val1, FILTER_VALIDATE_BOOLEAN);
                $result[$key1] = $val1;
            }
        }
        return $result;
    } else {
        if ($decoded = jsonDecodeAndValidate($data, true)) {
            return buildVirtualData($decoded);
        } else {
            return $data;
        }
    }
}


At the very least this will save you some calls to json_decode as the decoding is done once per iteration.

EDIT: I took another stab at it. I think you can boil it down to this.

#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, &amp;$build) {
    $data = jsonDecodeAndValidate($data);
    if(is_array($data) || is_object($data)){
        foreach($data as $key =&gt; $value){
            buildVirtualData($value, $build[$key]);
        }
    } else {
        if ($data === 'true')
          $data = true;
        if ($data === 'false')
          $data = false;
        $build = $data;
    }
}
# call to Function:
$build = [];
buildVirtualData($_POST, $build);
echo '&lt;pre&gt;';
echo json_encode($build, JSON_PRETTY_PRINT);
echo '&lt;/pre&gt;';


You just have to declare your array outside of the function.