我正在使用AJAX从javascript传递数组作为变量,然后将其分解回php中的数组。 但是,当数组的成员包含逗号时,php explode会将其解释为下一个成员的分隔符。 如何在数组字符串中转义逗号?
Javascript:
myArray = new Array();
myArray[0] = new Array("some text, some more text");
myArray[1] = new Array("more text");
postIt();
function postIt() {
var http = new XMLHttpRequest();
var url = "../mail/testSum.php";
var params = "wrongArray="+myArray;
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
//alert(http.responseText);
}
}
http.send(null);
}
php:
$myWrongArray = explode(',', $_GET["wrongArray"]);
$message = "The following questions were answered incorrectly: "."\r\n";
foreach($myWrongArray as $my_phpArray){
$message .= "\r\n".$my_phpArray;
}
$message = strip_tags($message);
由于第一个字符串中的逗号,因此php创建了数组的额外成员。如何才能摆脱逗号并迫使php只给我原始数组?
干杯
Charco