json_decode上的最后一个参数数组

时间:2013-10-01 14:28:11

标签: php json

大家好,我有这个字符串JSON,我用函数php json_decode($var,true)

解码它
[{"id":"4","name":"Elis"},{"id":"5","name":"Eilbert"}]

像我收到这个数组的结果

Array( [0] => Array ( [id] => 4 [name] => Elis ) 
       [1] => Array ( [id] => 5 [name] => Eilbert ))1

数组末尾的最后一个数字“1”是什么?为什么?我不想要它怎么能删除它?

在js上我传递一个用JSON.stringify()转换的数组,结果就像第一个json代码。

$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向我指出我的json字符串是用[]包裹的,我在js中传递了一个数组,并将其转换为JSON.stringify(),这是错误吗?

2 个答案:

答案 0 :(得分:4)

你在滥用print_r()

echo print_r($user);
  

如果要捕获print_r()的输出,请使用return参数。当此参数设置为TRUE时,print_r()将返回信息而不是打印信息。

     

当return参数为TRUE时,此函数将返回一个字符串。否则,返回值为TRUE

...当您将布尔值TRUE转换为字符串时,您将获得1

答案 1 :(得分:1)

Learn more about json_decode - 解码JSON字符串。

使用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

?>

Ref: