简体   繁体   English

在php中从json创建一个多维数组

[英]Creating a multidimensional array from json in php

I have a json file with a lot of records with fields for date , time and isApproved . 我有一个包含大量记录的json文件,其中包含datetime和已isApproved字段。 What I am trying to do is to create a json rray that has dates for all the records that are approved. 我想要做的是创建一个json rray,其中包含所有已批准的记录的dates And the dates have all the hours that are booked for the current date. 并且日期包含当前日期预订的所有小时数。

So from this... : 所以......:

 [{"fullName":"Jojn Petkobsky","userName":"user1","phone":"12415455","email":"ees@asd.com"date":"11\/16\/2016","time":"1pm ","reason":"ReaReasonReaeason","isApproved":0,"label":"warning","status":"Approved"},{"fullName":"Zoltan Heinkelman","userName":"user2","phone":"12415455","email":"ees@asdd.com"date":"11\/16\/2016","time":"2pm ","reason":"ReaReasonReaeason","isApproved":1,"label":"warning","status":"Approved"}{"fullName":"Jojn Petkobsky","userName":"user1","phone":"12415455","email":"ees@asd.com"date":"11\/16\/2016","time":"1pm ","reason":"ReaReasonReaeason","isApproved":1,"label":"warning","status":"Approved"},{"fullName":"Frank Heinkelman","userName":"user3","phone":"12415455","email":"ees@asddd.com"date":"10\/11\/2016","time":"2pm ","reason":"ReaReasonReaeason","isApproved":1,"label":"warning","status":"Approved"}]

...i can have something like this, so i can have all the booked hours for a given date. ...我可以拥有这样的东西,所以我可以拥有给定日期的所有预定时间。

{"12/11/16"😞
    {"time" :"10am"},
    {"time" :"1pm"},
    {"time" :"5pm"}
]
"12/10/16"😞
    {"time" :"9am"}
]
}

I tried with something like this, but couldn't really finish it : 我尝试过类似的东西,但无法完成它:

$string = file_get_contents("appointments.json");
$json_a = json_decode($string, true);
$date;
$datesTimes = array();
foreach($json_a as $json_r){
    if ($json_r['isApproved']==1) {
        $date = $json_r['date'];
        //another foreach?
    }
}

Any help will be appreciated! 任何帮助将不胜感激!

As I mentioned in the comments, your JSON is NOT valid so I have fixed it to a point to show how it can be done. 正如我在评论中提到,你的JSON是不是有效的,所以我有它固定在一个点来显示它是如何完成的。

//Fixed JSON as much as I could to show in example.

$json = '

 [

    {
        "fullName": "Jojn Petkobsky",
        "userName": "user1",
        "phone": "12415455",
        "email": "ees@asd.com",
        "date": "11\/16\/2016",
        "time": "1 pm",
        "reason": "ReaReasonReaeason",
        "isApproved": "0",
        "label": "warning",
        "status": "Approved"
    },

    {
        "fullName": "Kitson88",
        "userName": "user2",
        "phone": "122323325",
        "email": "eadasds@asasdasdd.com",
        "date": "11\/16\/2016",
        "time": "12 pm",
        "reason": "ReaReasonReaeason",
        "isApproved": "0",
        "label": "warning",
        "status": "Approved"
    },
{
        "fullName": "Jamie",
        "userName": "user2",
        "phone": "122323325",
        "email": "eadasds@asasdasdd.com",
        "date": "12\/16\/2016",
        "time": "8 pm",
        "reason": "ReaReasonReaeason",
        "isApproved": "0",
        "label": "warning",
        "status": "Approved"
    }



 ]

';


$array = json_decode($json, true);

//This will be you rnew Array for holding the needed data. 
$approved = [];

foreach ($array as $value) {

    if ($value['status'] === 'Approved') { //Any check really which will validate Approved

        if (array_key_exists($value['date'], $approved)) { //Check if key exists note** will only work on 1D Array

            array_push($approved[$value['date']], $value['time']); //If so, then append

        } else { //If not, then create it and add value

           $approved += [$value['date'] => [$value['time']]]; 

        }
    }
}


//Encode back to JSON
$newJson = json_encode($approved);

Output as JSON 输出为JSON

{
    "11\/16\/2016": ["1 pm", "12 pm"],
    "12\/16\/2016": ["8 pm"]
}

http://php.net/manual/en/function.array-key-exists.php http://php.net/manual/en/function.array-key-exists.php

http://php.net/manual/en/function.array-push.php http://php.net/manual/en/function.array-push.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM