note 96478 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:01:01 +0000
Subject: note 96478 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199441@lists.php.net to get a copy of this message
Note Submitter: ducnm0 at gmail dot com ---- A function to convert php array into JS object, supporting both object and array: <?php 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; } ?>

« previous php.notes (#199441) next »