根据键值取消设置数组

Unset Array based on key value

我这里有这个数组,我们称它为 $_products,我的目标是根据“activationdate”的键值删除该数组,如果它大于今天的日期。

{
    "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  & Milton Pinot Noir 2019 ",
    "image": "linkurl",
    "month": "December 2021",
    "activationdate": "2021-12-03",
    "wine1": "Apsley Gorge Pinot Noir 2018",
    "wine2": "Milton Pinot Noir 2019"
}

所以对于我的 PHP 循环

$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]);
}

但是,它不会取消设置数组?

您在循环中使用了不正确的变量。

替换

unset($_products[$month]);

unset($_product[$month]);

好的,我假设您有 JSON 饲料,例如 $product_feed,如下所示

$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  & Milton Pinot Noir 2019 ",
    "image": "linkurl",
    "month": "December 2021",
    "activationdate": "2021-12-03",
    "wine1": "Apsley Gorge Pinot Noir 2018",
    "wine2": "Milton Pinot Noir 2019"
}
]';

我已经使用 json_decode 函数将其转换为 PHP 数组,例如

$products = json_decode($json);

下一步 为了与当前日期进行比较,我已将当前日期转换为时间字符串,如下所示

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

主循环和未设置任务如

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

在这种情况下,您错过了它,您正在尝试取消设置键,而您应该使用键取消设置数组元素。

数组删除前后打印完整代码如下:

$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  & Milton Pinot Noir 2019 ",
        "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);

print_r 的输出是

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  & Milton Pinot Noir 2019 
            [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
        )

)

希望这就是您要找的!干杯!

你可以玩转代码 here

像这样使用 foreach

foreach($_products as $k=>$v ) 

if( $v->activationdate>$date_now) unset($_products[$k]);

您在 foreach 中使用了 month,但这也是一个关键,并且具有误导性。

全部代码:

<?php

$s = '[
{
    "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  & Milton Pinot Noir 2019 ",
    "image": "linkurl",
    "month": "December 2021",
    "activationdate": "2021-12-03",
    "wine1": "Apsley Gorge Pinot Noir 2018",
    "wine2": "Milton Pinot Noir 2019"
}
]';

$p=json_decode($s);

$date_now = date('Y-m-d'); //get today's date with format e.g 2021-01-02

foreach($p as $k=>$v ) if( $v->activationdate>$date_now) unset($p[$k]);

var_dump( $p);
?>

Teh playground