簡體   English   中英

根據鍵值取消設置數組

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

但是,它不會取消設置數組嗎?

您在循環內使用了不正確的變量。

代替

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 $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 function 將其轉換為 PHP 數組

$products = json_decode($json);

Next 為了與當前日期進行比較,我已將當前日期轉換為時間字符串,如下所示

$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 的 output 是

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
        )

)

我希望這就是你要找的! 干杯!

你可以在這里玩代碼

使用這樣的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);
?>

操場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM