CodeIgniter: problems with json_decode array

On my client side, I am sending an ajax request with jQuery in the following question:

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

On the server side, in the CodeIgniter controller, I get these values:

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

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

Also, if you think that I am better off reading the values ​​on the server side, please show me how to do this.

Thank.

+3
source share
2 answers

, , . , null json_decode, , - , double ".

+3

:

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

$array = explode(",", $var2);
+1

Source: https://habr.com/ru/post/1745314/


All Articles