array2json() - Convert PHP arrays to JSON

array2json() Logo

array2json is a PHP function that will convert the array given as its argument into a JSON string. The created JSON string will be returned. This is very useful in Ajax apps using JSON over XML. If you are using XML, you better off using my xml2array() JavaScript function.

Note: This is an old function - if you use a new version of PHP(5.2 or newer) you will get the json_encode() function. That can be used to convert arrays to a JSON string.


First you need a PHP array. For example...

$data = array(
    'success'    =>    "Sweet",
    'failure'    =>    false,
    'array'      =>    array(),
    'numbers'    =>    array(1,2,3),
    'info'       =>    array(
                        'name'    =>    'Binny',
                        'site'    =>    ''

Provide this array as the argument of the array2json() function...

$json = array2json($data);

The resulting string will be(actual output - the source says print array2json($data);)...

Fatal error: Uncaught Error: Call to undefined function array2json() in /home/binco/public_html/php/scripts/array2json/index.php:47 Stack trace: #0 {main} thrown in /home/binco/public_html/php/scripts/array2json/index.php on line 47