Feike Falkena
Feike Falkena

Reputation: 25

PHP - Advanced search value in multidimensional array

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.

Upvotes: 0

Views: 99

Answers (2)

Kankatala Krishna
Kankatala Krishna

Reputation: 76

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

Upvotes: 0

dWinder
dWinder

Reputation: 11642

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

Upvotes: 1

Related Questions