Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

config_json_encode()

Description

A subset json_encode function that only works on $config arrays but has none
of the version-to-version variability and other "unusual" behavior of PHP's.
implementation.

whose elements are UTF-8 encoded strings, booleans, numbers or arrays
of such elements and whose keys are either numbers or UTF-8 encoded
strings.
capabilities to encode

Parameters

ColumnTypeDefaultDescription
$config
mixed $config a configuration variables array. This *must* be an array

Return

json encoded version of $config or null if $config is beyond our

Location

include/plugin_functions.php lines 184 to 230

Definition

 
function config_json_encode($config)
    {
    
$i=0;
    
$simple_keys true;
    foreach (
$config as $name => $value)
        {
        if (!
is_numeric($name) || ($name != $i++))
            {
            
$simple_keys false;
            break;
            }
        }
    
$output $simple_keys?'[':'{';
    foreach (
$config as $name => $value)
        {
        if (!
$simple_keys)
            {
            
$output .= '"' config_encode($name) . '":';
            }
        if (
is_string($value))
            {
            
$output .= '"' config_encode($value) . '"';
            }
        elseif (
is_bool($value))
            {
            
$output .= $value?'true':'false';
            }
        elseif (
is_numeric($value))
            {
            
$output .= strval($value);
            }
        elseif (
is_array($value))
            {
            
$output .= config_json_encode($value);
            }
        else
            {
            return 
null// Give up; beyond our capabilities
            
}
        
$output .= ', ';
        }
    if (
substr($output, -2) == ', ')
        {
        
$output substr($output0, -2);
        }
    return 
$output . ($simple_keys?']':'}');
    }

This article was last updated 19th March 2024 06:05 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.