Re: Copying PHP array into a Javascript array

From: Date: Tue, 10 Apr 2007 20:56:09 +0000
Subject: Re: Copying PHP array into a Javascript array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-252471@lists.php.net to get a copy of this message
Otto Wyss wrote: > I don't know if I should ask this question here or in the JavaScript > group. I'll try it here since people usually are more helpful. > > I've an array of file names > > $files = getFiles ($d); > > but would like to use this array in a JavaScript array. How can I copy > this $files into a JavaScript variable? // if you have this available: http://php.net/manual/en/function.json-encode.php echo json_encode($files); // or do something like this (I'm guessing you didn't try to hard) $output = array() foreach ($files as $f) $output = "'".str_replace("'", "\'", $f)."'"; echo '['.join(',', $output).']'; // for the javascript needed to actually stick the js array notation string // into a js variable and use it your on your own. > > O. Wyss >

« previous php.general (#252471) next »