ajax数据是一个字符串而不是一个数组

时间:2012-10-29 17:51:31

标签: php jquery ajax

我使用jquery ajax将一些数据发送到php文件

$.ajax({
            type: 'GET',
            url: 'dosomething.php',
            data: {list:orderNew}
            });

将此发送到php

$list = $_GET['list'];

print $ list给了我这个:

  list[4]=null&list[1]=null&list[2]=4&list[12]=null&list[11]=null&list[3]=null

我希望把它作为一个数组,所以我可以在我的php文件

上做到这一点
 foreach($list as $key => $value) {
            if($value == "null"){
                $value = 0;
            }
}

如何将list []数据作为实际数组发送,以便php将其作为数组读取或将字符串转换为数组,以便我可以在我的php页面上进行操作?

2 个答案:

答案 0 :(得分:1)

尝试

parse_str($list, $arr);
print_r($arr);

输出

Array
(
    [list] => Array
        (
            [4] => null
            [1] => null
            [2] => 4
            [12] => null
            [11] => null
            [3] => null
        )

)

答案 1 :(得分:-1)

你必须在服务器端使用json_encode,然后在客户端使用:

$.ajax({
            type: 'JSON',
            url: 'dosomething.php',
            data: {list:orderNew}
            });