简体   繁体   中英

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 . What I am trying to do is to create a json rray that has dates for all the records that are approved. 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.

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

{
    "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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