根据日期删除数组索引

时间:2015-08-04 10:55:03

标签: php arrays symfony

我正在使用Symfony框架,我有一个控制器,其工作是获取RSS源。

我在一组数组中获取了已提取的Feed后,我想删除那些date大于我提供的日期的Feed。

所以这是我获取提要时获得的对象数组

Array
(
    [0] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Project Manager, Investments Business Management - Prudential - Madison, NJ
            [feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....

            From Prudential - 30 Jul 2015 20:00:31 GMT
            -  View all Madison jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Project-Manager-at-Prudential-in-Madison,-NJ-27eedeae3104d1be
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-07-30 20:00:31.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )

    [1] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Client Services Specialist - Prudential - Madison, NJ
            [feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....

            From Prudential - 03 Aug 2015 19:59:34 GMT
            -  View all Madison jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Client-Service-Specialist-at-Prudential-in-Madison,-NJ-51ad596876a01466
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-08-03 19:59:34.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )

    [2] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Manager, Client Services - Prudential - Madison, NJ
            [feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....

            From Prudential - 03 Aug 2015 19:59:32 GMT
            -  View all Madison jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Manager-at-Prudential-in-Madison,-NJ-c0452d58384711e7
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-08-03 19:59:32.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )

    [3] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Culture Products - STEMCELL Technologies Inc - United States
            [feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...

            From STEMCELL Technologies Inc - 01 Aug 2015 01:16:36 GMT
            -  View all  jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-f4c10a0e10852686
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-08-01 01:16:36.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )

    [4] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Separation Products - STEMCELL Technologies Inc - United States
            [feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...

            From STEMCELL Technologies Inc - 31 Jul 2015 01:13:35 GMT
            -  View all  jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-b8c62120268afb55
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-07-31 01:13:35.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )

    [5] => AppBundle\Entity\Rss Object
        (
            [id:AppBundle\Entity\Rss:private] => 
            [feedItemTitle:AppBundle\Entity\Rss:private] => Accounting Intern - Code Corp - Draper, UT
            [feedItemDescription:AppBundle\Entity\Rss:private] => Would love someone that is interested in International Accounting processes because this position will work with our China, Singapore & Europe office....

            From Code Corp - 31 Jul 2015 21:13:43 GMT
            -  View all Draper jobs
            [feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Accounting-Intern-at-Code-in-Draper,-UT-0e0aca45e988cc89
            [feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
                (
                    [date] => 2015-07-31 21:13:43.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

        )   
)

正如您将注意到上面数组中的date

我正在传递上面的数组和日期(不是date来自上面的数组,而是$date,我希望与数组日期匹配,以便在参数中排除Feed)到下面的函数

public function isValid($feed, $date)
{
    foreach ($feed as $item) {

        if ($item->getfeedItemPubDate()->date < $date) {
            echo $item->getfeedItemPubDate()->date;
            echo "<br>";
        }
    }

}

我可以获取其中date数组小于$date的Feed列表,但我坚持的是如何从阵列中删除这些Feed?然后将新的数组传回给控制器。

3 个答案:

答案 0 :(得分:1)

您可以通过取消设置功能删除数组项。要知道该项的关键,请更改一个foreach循环。如此,例如

public function isValid($feed, $date)
{
    foreach ($feed as $key => $item) 
        if ($item->getfeedItemPubDate()->date < $date)
            unset ($feed[$key]);   
}

答案 1 :(得分:1)

你也可以试试你提供回调函数的array_filter函数,例如

$feed = array_filter($feed, function($item) use($date) {
     return ($item->getfeedItemPubDate()->date < $date);
});

其中$date是您在

之前设置的变量

答案 2 :(得分:0)

尝试:

public function isValid($feed, $date)
{
foreach ($feed as $item) {

    if (strtotime($item->getfeedItemPubDate()->date) < strtotime($date)) {
        echo $item->getfeedItemPubDate()->date;
        echo "<br>";
    }
}

}