【问题标题】:Unset Array based on key value [closed]基于键值取消设置数组[关闭]
【发布时间】:2021-12-28 06:08:00
【问题描述】:

我这里有这个数组,我们称之为$_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 $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"
}

所以对于我的 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]);
}

但是,它并没有取消设置数组?

【问题讨论】:

  • 您正在将日期与字符串进行比较。 $_product['activationdate'] > $date_now
  • 嗯,奇怪。我确实尝试将两者与真/假进行比较。它回显了“真”,但没有删除数组。有什么建议吗?
  • 您能否确认您有 JSON 提要并使用 json_decode 将其转换为数组?
  • 先设置item为null,然后循环后,用户array_filter删除空值
  • 离题:无法重现。

标签: php loops foreach unset


【解决方案1】:

像这样使用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 $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"
}
]';

$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

【讨论】:

    【解决方案2】:

    好的,我假设您有 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 $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"
    }
    ]';
    

    我已经使用 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 $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);
    

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

    我希望这就是您想要的!干杯!

    你可以玩转代码here

    【讨论】:

    • strtotime() 不是必需的。
    • 感谢您创建此@Vantiya,但不幸的是,我仍然无法使其工作:/
    • @ccmanz 试试这个 $date_now = strtotime(date('Y-m-d'));而不是 $date_now = date('Y-m-d');并在循环中使用相同的 if 条件,例如 if( strtotime($_product['activationdate']) > $date_now ) 实际上,两个日期都应转换为 unix 时间戳进行比较。我确信您的代码中的条件不匹配,因此未执行未发送。
    【解决方案3】:

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

    替换

    unset($_products[$month]);

    unset($_product[$month]);
    

    【讨论】:

    • 我确实更新了这个 if( $_product['activationdate'] > $date_now ) unset( $_product[$month] );它仍然没有删除数组。如果我将值与真/假进行比较,则结果为真,但不会删除数组。
    • 所以这个页面应该作为错字关闭而不是回答?
    • @ccmanz,在循环中创建一个新数组,然后将字段放入该数组并检查新输出
    • @mickmackusa 虽然我犯了一个拼写错误。我仍然无法让它工作。我的道歉
    • @VishalParkash 我会试试的
    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2013-06-05
    • 2020-09-08
    相关资源
    最近更新 更多