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

时间:2016-11-20 18:34:05

标签: php arrays json multidimensional-array

我有一个包含大量记录的json文件,其中包含datetimeisApproved的字段。我要做的是为所有已批准的记录创建一个dates的json rray。并且日期包含当前日期预订的所有小时数。

所以......:

 [{"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"}]

...我可以拥有这样的东西,所以我可以获得给定日期的所有预定时间。

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

我试过这样的事情,但无法完成它:

$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?
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:4)

正如我在评论中提到的,您的JSON NOT 有效,所以我已将其修复到一定程度以显示如何完成。

//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);

输出为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-push.php