Hello guys i have this string JSON and I decode it with function php json_decode($var,true)
[{"id":"4","name":"Elis"},{"id":"5","name":"Eilbert"}]
Like result i receive this array
Array( [0] => Array ( [id] => 4 [name] => Elis )
[1] => Array ( [id] => 5 [name] => Eilbert ))1
What is it the last number "1" on the end of the array? why is there? I don't want have it how can i delete it?
On js i pass an array converted with JSON.stringify() and the result is like the first json code.
$user = json_decode($this->input->post('to'),true);
$conv_user_id = array();
foreach ($user as $po) {
$conv_user_id[] = $po['id'];
$conv_user_id[] = $id_user;
}
echo print_r($user);
@explosion phill pointed out to me that my json string is wrapped with []
i pass just an array in js converted it witj JSON.stringify(), wich is the error?
You're misusing print_r():
echo print_r($user);
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.
When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
... and when you cast boolean TRUE
to string you get 1
.
Learn more about json_decode — Decodes a JSON string.
Example Common mistakes using json_decode()
<?php
// the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
?>