note 88326 deleted from book.array by peehaa
| From: | peehaa@php.net | Date: | Mon, 08 Jul 2013 19:50:30 +0000 |
| Subject: | note 88326 deleted from book.array by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195510@lists.php.net to get a copy of this message | ||
Note Submitter: Endel Dreyer
----
Function to convert PHP arrays to JavaScript Array Object:
Useful in some cases.
<?php
function array2js($array,$show_keys=false)
{
$dimensoes = array();
$valores = array();
$total = count ($array)-1;
$i=0;
foreach ($array as $key => $value) {
if (is_array($value)) {
$dimensoes[$i] = array2js($value,$show_keys);
} else {
$dimensoes[$i] = '"'.addslashes($value).'"';
if ($show_keys) '"'.$key.'":'.$dimensoes[$i];
}
if ($i==0) $dimensoes[$i] = '['.$dimensoes[$i];
if ($i==$total) $dimensoes[$i].= ']';
$i++;
}
return implode(',',$dimensoes);
}
?>