使用php将json对象解析为json数组。

时间:2014-06-22 21:35:19

标签: php arrays json

我有人参加婚礼。我想要它,这样人们就可以提交他们的rsvp数据并将数据放入json数组中。然后我想把那个数组放到一个表中。

我在这里有这个阵列:

{
    "weddingRSVP": [
        {
            "success": true,
            "FirstName": "k",
            "LastName": "k",
            "Email": "k",
            "Phone": "k",
            "Comments": "kk",
            "GuestNumber": "0",
            "Guest1": "k",
            "Guest2": "k",
            "Guest3": "k"
        }
    ]
}

这是我运行代码时的结果:

{
    "0": {
        "success": true,
        "FirstName": "l",
        "LastName": "l",
        "Email": "l",
        "Phone": "l",
        "Comments": "l",
        "GuestNumber": "0",
        "Guest1": "l",
        "Guest2": "l",
        "Guest3": "l"
    },
    "weddingRSVP": [
        {
            "success": true,
            "FirstName": "k",
            "LastName": "k",
            "Email": "k",
            "Phone": "k",
            "Comments": "kk",
            "GuestNumber": "0",
            "Guest1": "k",
            "Guest2": "k",
            "Guest3": "k"
        }
    ]
}

这就是我想要的:

{
    "weddingRSVP": [
        {
            "success": true,
            "FirstName": "k",
            "LastName": "k",
            "Email": "k",
            "Phone": "k",
            "Comments": "kk",
            "GuestNumber": "0",
            "Guest1": "k",
            "Guest2": "k",
            "Guest3": "k"
        },
        {
            "success": true,
            "FirstName": "l",
            "LastName": "l",
            "Email": "l",
            "Phone": "l",
            "Comments": "l",
            "GuestNumber": "0",
            "Guest1": "l",
            "Guest2": "l",
            "Guest3": "l"
        }
    ]
}

这就是我所有的PHP:

<?php
header('Content-type: text/javascript');

$fname =$lname= $email = $tel = $comment = $guest = $guestnum =$guest1=$guest2=$guest3= "";

$json = array(

    'success' => false,
    'FirstName' => 0,
    'LastName' => 0,
    'Email' => 0,
    'Phone' => 0,
    'Comments' => 0,
    'GuestNumber' => 0,
    'Guest1' => 0,
    'Guest2' => 0,
    'Guest3' => 0
    );


    if (isset($_POST['fname'],$_POST['lname'],
                $_POST['email'],$_POST['tel'],
                $_POST['comment'],$_POST['guestnum'],
                $_POST['guest3'],$_POST['guest2'],
                $_POST['guest3'])) 
    {
        $fname = test_input($_POST["fname"]);
        $lname = test_input($_POST["lname"]);
        $email = test_input($_POST["email"]);
        $tel =   test_input($_POST["tel"]);
        $comment = test_input($_POST["comment"]);
        $guestnum = test_input($_POST["guestnum"]);
        $guest1 = test_input($_POST["guest1"]);
        $guest2 = test_input($_POST["guest2"]);
        $guest3 = test_input($_POST["guest3"]);

        $json['success'] = true;
        $json['FirstName'] = $fname;
        $json['LastName'] = $lname;
        $json['Email'] = $email;
        $json['Phone'] = $tel;
        $json['Comments'] = $comment;
        $json['GuestNumber'] = $guestnum;
        $json['Guest1'] = $guest1;
        $json['Guest2'] = $guest2;
        $json['Guest3'] = $guest3;

    }
        $file = 'weddingRSVP.json';
        $data = json_decode(file_get_contents($file),1);
        $data[] = $json;
        file_put_contents($file, json_encode($data));



    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        return $data;
    }
    ?>

尽管这段代码发生了大部分魔力:

        $file = 'weddingRSVP.json';
        $data = json_decode(file_get_contents($file),1);
        $data[] = $json;
        file_put_contents($file, json_encode($data));

感谢任何帮助过的人!

1 个答案:

答案 0 :(得分:0)

我认为问题在于

$data[] = $json;

而是尝试

$data['weddingRSVP'][] = $json

希望有所帮助

    $file = 'weddingRSVP.json';
    $data = json_decode(file_get_contents($file),1);
    $data['weddingRSVP'][] = $json
    file_put_contents($file, json_encode($data));