JavaScript PHP Programming

PHP – Converting an Array to JSON

February 4, 2011

Every now and then I stumble upon a tidbit of information that just makes me want to kick myself. Today was one of those days. Just by chance I ended up at an entry on PHP.net while Googling another issue. Once I realized what this function was capable of I was torn between thinking “Wow, this is awesome!” and “Wow, I’m a dumbass!”

$array = array (
    'alpha'=>4,
    'beta'=>9,
    'gamma'=>3
);
echo json_encode($array)

You should then see something like the following:

{"alpha":4,"beta":9,"gamma":3}

If you were manually processing the array and building a JSON object like I was, assume the face palm position.

Only registered users can comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.