phploopsforeachunset

Unset Array based on key value


I have this array here let's call it $_products, my goal is to remove the array based on the key value of "activationdate" if it's greater than today's date.

{
    "id": "1388",
    "name": "June 2019 -  2014 Kate Hill & 2014 Pressing Matters",
    "image": "linkurl",
    "month": "June 2019",
    "activationdate": "2019-06-01",
    "wine1": "2014 Kate Hill Pinot Noir",
    "wine2": "2014 Pressing Matters Pinot Noir"
},

{   //I WANT TO REMOVE THIS ARRAY
    "id": "8421",
    "name": "December 2021 Releases: Apsley Gorge Pinot Noir 2018 $65 & Milton Pinot Noir 2019 $38",
    "image": "linkurl",
    "month": "December 2021",
    "activationdate": "2021-12-03",
    "wine1": "Apsley Gorge Pinot Noir 2018",
    "wine2": "Milton Pinot Noir 2019"
}

So for my PHP loop

$date_now = date('Y-m-d'); //get today's date with format e.g 2021-01-02
foreach( $_products as $month => $_product ) {

if( $_product['activationdate'] > $date_now  )
   unset($_products[$month]);
}

However, it doesn't unset the array?


Solution

  • Ok I presume that you have JSON feed for example $product_feed like follows

    $json = $json = '[
    {
        "id": "1388",
        "name": "June 2019 -  2014 Kate Hill & 2014 Pressing Matters",
        "image": "linkurl",
        "month": "June 2019",
        "activationdate": "2019-06-01",
        "wine1": "2014 Kate Hill Pinot Noir",
        "wine2": "2014 Pressing Matters Pinot Noir"
    },
    {
        "id": "8421",
        "name": "December 2021 Releases: Apsley Gorge Pinot Noir 2018 $65 & Milton Pinot Noir 2019 $38",
        "image": "linkurl",
        "month": "December 2021",
        "activationdate": "2021-12-03",
        "wine1": "Apsley Gorge Pinot Noir 2018",
        "wine2": "Milton Pinot Noir 2019"
    }
    ]';
    

    I have converted that into PHP array using json_decode function like

    $products = json_decode($json);
    

    Next To compare with the current date I have converted current date to time string as follow

    $current_date = strtotime(date('Y-m-d'));
    

    The main loop and unsetting task is as

    foreach ($products as $month => $product) {
        if(strtotime($product->activationdate) > $current_date) { // Date comparison
            unset($products[$month]); // Here you made a mistake
        }
    }
    

    In this case what you've missed it you're trying to unset key where you supposed to unset array element with the key.

    The full code with print before and after array deletion as follow:

    $json = '[
        {
            "id": "1388",
            "name": "June 2019 -  2014 Kate Hill & 2014 Pressing Matters",
            "image": "linkurl",
            "month": "June 2019",
            "activationdate": "2019-06-01",
            "wine1": "2014 Kate Hill Pinot Noir",
            "wine2": "2014 Pressing Matters Pinot Noir"
        },
        {
            "id": "8421",
            "name": "December 2021 Releases: Apsley Gorge Pinot Noir 2018 $65 & Milton Pinot Noir 2019 $38",
            "image": "linkurl",
            "month": "December 2021",
            "activationdate": "2021-12-03",
            "wine1": "Apsley Gorge Pinot Noir 2018",
            "wine2": "Milton Pinot Noir 2019"
        }
    ]';
    $products = json_decode($json);
    $current_date = strtotime(date('Y-m-d'));
    print_r($products);
    foreach ($products as $month => $product) {
        if(strtotime($product->activationdate) > $current_date) { // Date comparison
            unset($products[$month]); // Here you made a mistake
        }
    }
    print_r($products);
    

    The output of the print_r is

    Array
    (
        [0] => stdClass Object
            (
                [id] => 1388
                [name] => June 2019 -  2014 Kate Hill & 2014 Pressing Matters
                [image] => linkurl
                [month] => June 2019
                [activationdate] => 2019-06-01
                [wine1] => 2014 Kate Hill Pinot Noir
                [wine2] => 2014 Pressing Matters Pinot Noir
            )
    
        [1] => stdClass Object
            (
                [id] => 8421
                [name] => December 2021 Releases: Apsley Gorge Pinot Noir 2018 $65 & Milton Pinot Noir 2019 $38
                [image] => linkurl
                [month] => December 2021
                [activationdate] => 2021-12-03
                [wine1] => Apsley Gorge Pinot Noir 2018
                [wine2] => Milton Pinot Noir 2019
            )
    
    )
    Array
    (
        [0] => stdClass Object
            (
                [id] => 1388
                [name] => June 2019 -  2014 Kate Hill & 2014 Pressing Matters
                [image] => linkurl
                [month] => June 2019
                [activationdate] => 2019-06-01
                [wine1] => 2014 Kate Hill Pinot Noir
                [wine2] => 2014 Pressing Matters Pinot Noir
            )
    
    )
    

    I hope this is what you're looking for! Cheers!

    You can play around the code here