note 88577 deleted from book.array by danbrown

From: Date: Thu, 29 Jan 2009 22:41:53 +0000
Subject: note 88577 deleted from book.array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149958@lists.php.net to get a copy of this message
Note Submitter: JustinB at harvest dot org ---- @Endel Dreyer: In PHP 5.2.0+ (or PECL json 1.2+), you can just use json_encode(): <?php // simple arrays in PHP are turned into JavaScript array objects $myArr = array('val1','val2','val3'); echo json_encode($myArr); // Output: ["val1","val2","val3"] // arrays with keys are turned into JS Object notation (since JS Array doesn't allow for keys) $myKeyArr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'); echo json_encode($myKeyArr); // Output: {"key1":"val1","key2":"val2","key3":"val3"} ?> In addition to arrays, json_encode() is great for converting simple objects (simple properties only), strings, numbers, boolean flags, etc. (anything but resources).

« previous php.notes (#149958) next »