note 76525 added to ref.array
| From: | g4wx3 at osu1 dot php dot net | Date: | Thu, 19 Jul 2007 09:16:55 +0000 |
| Subject: | note 76525 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128714@lists.php.net to get a copy of this message | ||
I needed a function to convert a php array into a javascript array.
No problem i found it on "the net".
But the function i found wasn't good enough, instead of return a string with javascript-array
it echoed directly everything.
I wanted to write the string to a file, when calling the function out of my function libary.
Secondly, there where minor "bugs" in the script, when you're original array
contained characters like line breaks(\r\n,..), or quotes('), it would hack up the javascript
array
Sow, i changed the function and fixed the bug.
<?php
//SUPER COOL : http://www.communitymx.com/content/article.cfm?page=3&cid=7CD16
//Checkout: REVERSE: http://www.hscripts.com/tutorials/php/jsArrayToPHP.php
//Convert a PHP array to a JavaScript one (rev. 4)
//Changlog by g4wx3: echo replaced by $output, added function output
function output($string) //make javascript ready
{
$string = str_replace( array( '\\' , '\'' ), array('\\\\',
'\\\'') , $string); //-> for javascript array
$string = str_replace( array("\r\n", "\r", "\n") ,
'<br>' , $string); //nl2br
return $string;
}
function arrayToJS4($array, $baseName ) {
//Write out the initial array definition
//v4 echo ($baseName . " = new Array(); \r\n ");
$output = $baseName . " = new Array(); \r\n ";
//Reset the array loop pointer
reset ($array);
//Use list() and each() to loop over each key/value
//pair of the array
while (list($key, $value) = each($array)) {
if (is_numeric($key)) {
//A numeric key, so output as usual
$outKey = "[" . $key . "]";
} else {
//A string key, so output as a string
$outKey = "['" . $key . "']";
}
if (is_array($value)) {
//The value is another array, so simply call
//another instance of this function to handle it
$output .= arrayToJS4($value, $baseName . $outKey);
} else {
//Output the key declaration
//v4 echo ($baseName . $outKey . " = ");
$output .= $baseName . $outKey . " = ";
//Now output the value
if (is_string($value)) {
//Output as a string, as we did before
//v4 echo ("'" . output($value) . "'; \r\n ");
$output .= "'" . output($value) . "'; \r\n ";
} else if ($value === false) {
//Explicitly output false
//v4 echo ("false; \r\n");
$output .= "false; \r\n";
} else if ($value === NULL) {
//Explicitly output null
//v4 echo ("null; \r\n");
$output .= "null; \r\n";
} else if ($value === true) {
//Explicitly output true
//v4 echo ("true; \r\n");
$output .= "true; \r\n";
} else {
//Output the value directly otherwise
//v4 echo ($value . "; \r\n");
$output .= $value . "; \r\n";
}
}
}
return $output;
}
?>
You can use this for printing $_GET array, for example
----
Server IP: 87.238.162.147
Probable Submitter: 81.244.61.34
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/76525
Del: integrated -- https://master.php.net/note/delete/76525/integrated
Del: useless -- https://master.php.net/note/delete/76525/useless
Del: bad code -- https://master.php.net/note/delete/76525/bad+code
Del: spam -- https://master.php.net/note/delete/76525/spam
Del: non-english -- https://master.php.net/note/delete/76525/non-english
Del: in docs -- https://master.php.net/note/delete/76525/in+docs
Del: other reasons-- https://master.php.net/note/delete/76525
Reject -- https://master.php.net/note/reject/76525
Search -- https://master.php.net/manage/user-notes.php