向JSON对象追加数据问题


append data to JSON object issue

我试图将数据添加到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"],
    )));

检查这个:

$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"],
                        )
    ));