将php数组转换为Javascript

我如何以这种格式转换PHP数组

Array

(

[0] => 001-1234567

[1] => 1234567

[2] => 12345678

[3] => 12345678

[4] => 12345678

[5] => AP1W3242

[6] => AP7X1234

[7] => AS1234

[8] => MH9Z2324

[9] => MX1234

[10] => TN1A3242

[11] => ZZ1234

)

到以下格式的Javascript数组?

var cities = [

"Aberdeen",

"Ada",

"Adamsville",

"Addyston",

"Adelphi",

"Adena",

"Adrian",

"Akron",

"Albany"

];

回答:

您不再需要以下内容

如果您没有PHP 5.2,则可以使用以下代码:

function js_str($s)

{

return '"' . addcslashes($s, "\0..\37\"\\") . '"';

}

function js_array($array)

{

$temp = array_map('js_str', $array);

return '[' . implode(',', $temp) . ']';

}

echo 'var cities = ', js_array($php_cities_array), ';';

以上是 将php数组转换为Javascript 的全部内容, 来源链接: utcz.com/qa/407680.html

回到顶部