首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据附加到JSON对象问题

将数据附加到JSON对象问题
EN

Stack Overflow用户
提问于 2014-03-13 18:39:27
回答 1查看 57关注 0票数 0

我正在尝试向JSON对象添加数据。输出结果不显示附加的数据"people3“。如果我做得对,请告诉我好吗?

代码语言:javascript
复制
$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 ));

更新代码:

代码语言:javascript
复制
array_push($postArray["persons"]['people'],
    array("people3"=> array(

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

    )));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-13 18:49:11

检查一下这个:

代码语言:javascript
复制
$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 :-

代码语言:javascript
复制
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] => 
                        )

                )

        )

)

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

代码语言:javascript
复制
array_push($postArray['persons']['people'], 
    array(
                    "people3"=> array(
                       "first_name"=>$_POST["first-2"],
                       "last_name"=>$_POST["last-2"],
                        )
    ));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22387942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档