将数据附加到JSON对象问题

时间:2014-03-13 18:39:28

标签: php json

我正在尝试将数据添加到JSON对象。输出结果不显示附加数据“people3”。如果我做得对,请你告诉我。

$postArray = array(
    "persons" =>  array(
        "person" => array(
                 "i_date"=> $DatabaseDate,
              "i_location"=>$_POST["location"],
              "i_summary"=>$_POST["summary"]
        ),
        "people" => array(
                "people1"=> array(
                       "first_name"=> $_POST["first-1"],
                       "last_name"=>$_POST["last-1"]
                      ),
                "people2"=> array(
                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],
                        )
                )
    )
);



array_push($postArray['people'], 
    array(
                    "people3"=> array(
                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],
                        )
    ));



var_dump(json_encode( $postArray ));

更新的代码:

array_push($postArray["persons"]['people'],
    array("people3"=> array(

                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],

    )));

1 个答案:

答案 0 :(得分:0)

检查一下:

$postArray = array(
    "persons" =>  array(
        "person" => array(
                 "i_date"=> $DatabaseDate,
              "i_location"=>$_POST["location"],
              "i_summary"=>$_POST["summary"]
        ),
        "people" => array(
                "people1"=> array(
                       "first_name"=> $_POST["first-1"],
                       "last_name"=>$_POST["last-1"]
                      ),
                "people2"=> array(
                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],
                        )
                )
    )
);



$postArray["persons"]['people']['people3']=
    array(

                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],

    );



var_dump(json_encode( $postArray ));

php中的输出:print_r: -

Array
(
    [persons] => Array
        (
            [person] => Array
                (
                    [i_date] => 
                    [i_location] => 
                    [i_summary] => 
                )

            [people] => Array
                (
                    [people1] => Array
                        (
                            [first_name] => 
                            [last_name] => 
                        )

                    [people2] => Array
                        (
                            [first_name] => 
                            [last_name] => 
                        )

                    [people3] => Array
                        (
                            [first_name] => 
                            [last_name] => 
                        )

                )

        )

)

正如其他人所说,这也是正确的。

array_push($postArray['persons']['people'], 
    array(
                    "people3"=> array(
                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],
                        )
    ));