note 96478 added to ref.array
| From: | ducnm0 at gmail dot com | Date: | Mon, 01 Mar 2010 13:08:15 +0000 |
| Subject: | note 96478 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166466@lists.php.net to get a copy of this message | ||
A function to convert php array into JS object, supporting both object and array:
static function is_assoc($array) {
foreach ( array_keys ( $array ) as $k => $v ) {
if ($k !== $v)
return true;
}
return false;
}
static function arrayPHPToJS($data) {
if (is_null($data)) return 'null';
if (is_string($data)) return "'" . $data . "'";
if (self::is_assoc($data)) {
$a=array();
foreach ($data as $key => $val )
$a[]="'" . $key . "' :" .self::arrayPHPtoJS($val);
return "{" . implode ( ', ', $a ) . "}";
}
if (is_array($data)) {
$a=array();
foreach ($data as $val )
$a[]=self::arrayPHPtoJS($val);
return "[" . implode ( ', ', $a ) . "]";
}
return $data;
}
----
Server IP: 69.147.83.197
Probable Submitter: 137.132.3.7
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/96478
Del: integrated -- https://master.php.net/note/delete/96478/integrated
Del: useless -- https://master.php.net/note/delete/96478/useless
Del: bad code -- https://master.php.net/note/delete/96478/bad+code
Del: spam -- https://master.php.net/note/delete/96478/spam
Del: non-english -- https://master.php.net/note/delete/96478/non-english
Del: in docs -- https://master.php.net/note/delete/96478/in+docs
Del: other reasons-- https://master.php.net/note/delete/96478
Reject -- https://master.php.net/note/reject/96478
Search -- https://master.php.net/manage/user-notes.php