PHP – Converting an Array to JSON

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.


Tags: , ,

  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

One Response to “PHP – Converting an Array to JSON”

  1. […] an entry on PHP.net while Googling another issue. Once I realized what this function was capable… [full post] FettesPS FettesPS javascriptphpprogrammingjson 0 0 0 0 […]

Leave a Reply