Teodora Georgieva
Teodora Georgieva

Reputation: 145

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":"[email protected]"date":"11\/16\/2016","time":"1pm ","reason":"ReaReasonReaeason","isApproved":0,"label":"warning","status":"Approved"},{"fullName":"Zoltan Heinkelman","userName":"user2","phone":"12415455","email":"[email protected]"date":"11\/16\/2016","time":"2pm ","reason":"ReaReasonReaeason","isApproved":1,"label":"warning","status":"Approved"}{"fullName":"Jojn Petkobsky","userName":"user1","phone":"12415455","email":"[email protected]"date":"11\/16\/2016","time":"1pm ","reason":"ReaReasonReaeason","isApproved":1,"label":"warning","status":"Approved"},{"fullName":"Frank Heinkelman","userName":"user3","phone":"12415455","email":"[email protected]"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!

Upvotes: 3

Views: 484

Answers (1)

Kitson88
Kitson88

Reputation: 2950

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": "[email protected]",
        "date": "11\/16\/2016",
        "time": "1 pm",
        "reason": "ReaReasonReaeason",
        "isApproved": "0",
        "label": "warning",
        "status": "Approved"
    },

    {
        "fullName": "Kitson88",
        "userName": "user2",
        "phone": "122323325",
        "email": "[email protected]",
        "date": "11\/16\/2016",
        "time": "12 pm",
        "reason": "ReaReasonReaeason",
        "isApproved": "0",
        "label": "warning",
        "status": "Approved"
    },
{
        "fullName": "Jamie",
        "userName": "user2",
        "phone": "122323325",
        "email": "[email protected]",
        "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

Upvotes: 4

Related Questions