0

I'm making a PHP page that can display the BackupJobs of the last week. I get all of the backup-jobs from an API that puts it into a JSON format.

I'm stuck on the point where I have a BackupSetID and want to echo all BackupJobs that matches with the BackupSetID onto a page.

Array:

{

    "Status": "OK",
    "Data": [
        {
            "BackupJob": [
                "2019-07-30-16-00-00",
                "2019-07-30-17-00-00",
                "2019-07-30-18-00-00",
                "2019-07-30-21-00-00",
                "2019-07-30-23-00-00",
                "2019-07-31-08-00-00",
                "2019-07-31-09-00-00",
                "2019-07-31-10-00-00",
                "2019-07-31-11-00-00",
                "2019-07-31-12-00-00",
                "2019-07-31-13-00-00",
                "2019-07-31-14-00-00",
                "2019-07-31-15-00-00",
                "2019-07-31-16-00-00",
                "2019-07-31-17-00-00",
                "2019-07-31-18-00-00",
                "2019-07-31-21-00-00",
                "2019-07-31-23-00-00",
                "2019-08-01-08-00-00",
                "2019-08-01-09-00-00"
            ],
            "BackupSetID": "1369227132163",
            "Removed": false
        },
        {
            "BackupJob": [
                "2019-07-22-18-00-00",
                "2019-07-22-22-00-00",
                "2019-07-23-10-00-00",
                "2019-07-23-14-00-00",
                "2019-07-23-18-00-00",
                "2019-07-23-22-00-00",
                "2019-07-24-10-00-00",
                "2019-07-24-14-00-00",
                "2019-07-24-18-00-00",
                "2019-07-24-22-00-00",
                "2019-07-25-10-00-00",
                "2019-07-25-14-00-00",
                "2019-07-25-18-00-00",
                "2019-07-25-22-00-00",
                "2019-07-26-10-00-00",
                "2019-07-26-14-00-00",
                "2019-07-26-18-00-00",
                "2019-07-26-22-00-00",
                "2019-07-27-10-00-00",
                "2019-07-27-14-00-00",
                "2019-07-27-18-00-00",
                "2019-07-27-22-00-00",
                "2019-07-28-10-00-00",
                "2019-07-28-14-00-00",
                "2019-07-28-18-00-00",
                "2019-07-28-22-00-00",
                "2019-07-29-10-00-00",
                "2019-07-29-14-00-00",
                "2019-07-29-18-00-00",
                "2019-07-29-22-00-00",
                "2019-07-30-10-00-00",
                "2019-07-30-14-00-00",
                "2019-07-30-18-00-00",
                "2019-07-30-22-00-00",
                "2019-07-31-10-00-00",
                "2019-07-31-14-00-00",
                "2019-07-31-18-00-00",
                "2019-07-31-22-00-00"
            ],
            "BackupSetID": "1369227067996",
            "Removed": false
        }
    ]

}

How would I display only the BackupJobs of the last week with (for example): BackupSetID = 1369227067996?

I tried to search between values. But without a result.

11
  • Can there be multiple matches with BackupSetID ? In other words, can there be multiple BackupSetID to match with? Commented Aug 1, 2019 at 7:19
  • How do you want the final array be look like? Commented Aug 1, 2019 at 7:21
  • @vivek_23 Yes there can be multiple. Commented Aug 1, 2019 at 7:21
  • @FeikeFalkena Ok. What does this key mean "Removed": false? Does it have any significance? Commented Aug 1, 2019 at 7:25
  • 1
    Did you try simple foreach loop? what is your desire output? Commented Aug 1, 2019 at 7:26

2 Answers 2

1

You can combine the 2 condition in the same foreach loop:

$backupSetID = 1369227067996; // id to search on
$lastWeekTime = date("Y-m-d-h-i", strtotime('sunday last week'));
foreach ($arr["data"] as $e) {
    if ($e["BackupSetID"] == $backupSetID) { // check if ID match
        foreach($e["BackupJob"] as $job)
            if ($job > $lastWeekTime) echo "Found $job \n"; // check for last week
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Why didnt I think that by myself :D Here is my code I made from it pastebin.com/JRQbzQE2
0

You need to convert json string to array using json_decode function.

$array = json_decode($data,true);
$backupSetID = "1369227067996";
$required_data = [];
if(count($array)){
    foreach ($array['Data'] as $index => $value) {        
        if($value["BackupSetID"] == $backupSetID){
            $required_data = $value['BackupJob'];
        }
    }
}

Required Result: print_r($required_data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.