Pregunta

I'm trying to debug a PHP application, and as a section of the debug process, I passed print_r($GLOBALS) through the AJAX request to my browser. However, I'd prefer to see it in native JSON form because it comes out better in the browser. I'm trying to use the following snippet of code:

json_encode($GLOBALS);

but I've found it returns bool(false). The JSON documentation says "Returns a JSON encoded string on success or FALSE on failure." But what about $GLOBALS makes it fail? Is it the recursive $GLOBALS['GLOBALS']?

I was thinking as an alternative to loop over $GLOBALS and put that in an array, but that seems quite pointless when the point of json_encode is to encode an array.

¿Fue útil?

Solución

Upon testing this myself, it appears json_encode() can't handle recursion such as what's provided in $GLOBALS['GLOBALS']... etc.

So a trick(?) you can do is:

json_encode(array_slice($GLOBALS, 1));

Which will skip past $GLOBALS['GLOBALS'] and continue encoding the rest of the array.

*EDIT: $GLOBALS['GLOBALS'] appears first for me when printing this array, but a better way is to find where $GLOBALS['GLOBALS'] appears and skip that element entirely.

Otros consejos

I propose a way where the position of GLOBALS is not important:

json_encode(array_intersect_key($GLOBALS,array_flip(array("_GET", "_POST", "_FILES", "_COOKIE"))));

or a better way:

$new_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($new_array));
json_encode(array_splice($new_array, $index, $index-1));

The fact that $GLOBALS contains reference to itself results in an infinite recursion that json_encode can't handle because it exceeds the maximum depth of 512 and thus will return false by default.

The solution would be creating a copy of $GLOBALS without self-referencing, the function below references superglobals _GET _POST _SERVER .. only, assuming you don't use $_ in your variables naming conventions:

function getEncodableGlobals()
{
    $g = [];

    foreach ($GLOBALS as $key => &$val)
    {
        if ($key[0] == '_')
        {
            $g[$key] = &$val;
        }
    }

    return $g;
}

Notice that $g doesn't hold copies but only references to variables, just like $GLOBALS does. If you wish to include all the variables in the global scope simply change the condition to exclude the troublesome reference:

...
    if ($key !== 'GLOBALS')
...

Now you can safely encode it:

json_encode(
    getEncodableGlobals()
);

You could also do it the array_filter way and copy the variables.

$encodableGlobals = array_filter($GLOBALS, function($key) {
    return $key !== 'GLOBALS';
}, ARRAY_FILTER_USE_KEY);

json_encode can't handle recursion such as what's provided in $GLOBALS['GLOBALS']... etc.

JSON.stringify is same! you will retrieve error : JSON.stringify error because recursion

So in php you can create a new variable, example $globals which holds an array of $GLOBALS.

And, unset the recursive $globals['GLOBALS'].

$globals = $GLOBALS;
unset($globals['GLOBALS']);
echo json_encode($globals);

And... Congrats!! Now you can json_encode $GLOBALS variable!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top