note 88577 added to book.array
| From: | JustinB at harvest dot org | Date: | Thu, 29 Jan 2009 22:41:00 +0000 |
| Subject: | note 88577 added to book.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149957@lists.php.net to get a copy of this message | ||
@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).
----
Server IP: 209.41.74.194
Probable Submitter: 64.118.163.18
----
Manual Page -- http://www.php.net/manual/en/book.array.php
Edit -- https://master.php.net/note/edit/88577
Del: integrated -- https://master.php.net/note/delete/88577/integrated
Del: useless -- https://master.php.net/note/delete/88577/useless
Del: bad code -- https://master.php.net/note/delete/88577/bad+code
Del: spam -- https://master.php.net/note/delete/88577/spam
Del: non-english -- https://master.php.net/note/delete/88577/non-english
Del: in docs -- https://master.php.net/note/delete/88577/in+docs
Del: other reasons-- https://master.php.net/note/delete/88577
Reject -- https://master.php.net/note/reject/88577
Search -- https://master.php.net/manage/user-notes.php