开发者

CodeIgniter: json_decode array issues

On my client side I'm sending an ajax request w开发者_JAVA技巧ith jQuery in the following matter:

$.post(script.php, { "var1":"something", "var2":"[1,2,3]" }, function(data) { }, "json");

On the server side, in the CodeIgniter's controller I'm receiving the values like so:

$var1 = trim($this->input->post('var1'));
$var2 = trim($this->input->post('var2'));

My question is how do I convert the string in $var2 into a PHP array. I tried using json_decode($var2, true) but it returns a null since "[1,2,3]" is not a legal JSON string by itself.

Also, if you believe there is a better way for me to read the values on the server-side please show me how.

Thank you.


As @Galen stated in his comment to my question, it works. The reason I got a null from json_decode is because it tried it with a non-array value, which requires a double ".


You could do this:

$var2 = trim($this->input->post('var2'), "[]");

$array = explode(",", $var2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜