[英]CodeIgniter: json_decode array issues
On my client side I'm sending an ajax request with jQuery in the following matter: 在我的客户端,我在以下问题中使用jQuery发送ajax请求:
$.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: 在服务器端,在CodeIgniter的控制器中,我收到的值如下:
$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. 我的问题是如何将$var2
的字符串转换为PHP数组。 I tried using json_decode($var2, true)
but it returns a null
since "[1,2,3]" is not a legal JSON string by itself. 我尝试使用json_decode($var2, true)
但它返回null
因为“[1,2,3]”本身不是合法的JSON字符串。
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. 正如@Galen在他对我的问题的评论中所说,它有效。 The reason I got a null
from json_decode
is because it tried it with a non-array value, which requires a double "
. 我从json_decode
获得null
的原因是因为它尝试使用非数组值,这需要一个双"
。
You could do this: 你可以这样做:
$var2 = trim($this->input->post('var2'), "[]");
$array = explode(",", $var2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.