从多维数组中获取具有最高日期时间值的数组元素

时间:2017-02-10 13:53:07

标签: php arrays multidimensional-array ebay-api

我有一个像这样的数组

Array
(
    [4621162015] => Array
        (
            [0] => Array
                (
                    [itemId] => 220929621668
                    [itemName] => Teak Protector - 1ltr water-based teak wood protector - maintain honey colour!
                    [itemPrice] => 24.99
                    [itemCurrencySign] => £
                    [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/qI8AAOSwt7pXMKHB/$_1.JPG?set_id=880000500F
                    [itemImages] => Array
                        (
                            [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/qI8AAOSwt7pXMKHB/$_1.JPG?set_id=880000500F
                            [1] => http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/70cAAOSwcL5XMKC6/$_1.JPG?set_id=880000500F
                        )

                    [itemQuantity] => 257
                    [itemStartTime] => 2012-01-10T16:40:03.000Z
                    [itemEndTime] => 2017-02-12T16:40:03.000Z
                    [itemUrl] => http://www.ebay.com/itm/Teak-Protector-1ltr-water-based-teak-wood-protector-maintain-honey-colour-/220929621668
                    [itemCategoryId] => 4621162015
                    [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=4621162015
                )

            [1] => Array
                (
                    [itemId] => 220929626118
                    [itemName] => Teak Patinizer - 1ltr patinizer to maintain the silver-patina or weathered look!
                    [itemPrice] => 24.99
                    [itemCurrencySign] => £
                    [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/1bwAAOSwYmZXMKE-/$_1.JPG?set_id=880000500F
                    [itemImages] => Array
                        (
                            [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/1bwAAOSwYmZXMKE-/$_1.JPG?set_id=880000500F
                            [1] => http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/A~4AAOSwubRXMKE8/$_1.JPG?set_id=880000500F
                        )

                    [itemQuantity] => 202
                    [itemStartTime] => 2012-01-10T16:49:41.000Z
                    [itemEndTime] => 2017-02-12T16:49:41.000Z
                    [itemUrl] => http://www.ebay.com/itm/Teak-Patinizer-1ltr-patinizer-maintain-silver-patina-weathered-look-/220929626118
                    [itemCategoryId] => 4621162015
                    [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=4621162015
                )

            [2] => Array
                (
                    [itemId] => 220929631080
                    [itemName] => Teak Cleaner - 1ltr bring back the original colour of new teak/hardwood!
                    [itemPrice] => 19.5
                    [itemCurrencySign] => £
                    [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F
                    [itemImages] => Array
                        (
                            [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F
                            [1] => http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F
                        )

                    [itemQuantity] => 204
                    [itemStartTime] => 2012-01-10T16:59:50.000Z
                    [itemEndTime] => 2017-02-12T16:59:50.000Z
                    [itemUrl] => http://www.ebay.com/itm/Teak-Cleaner-1ltr-bring-back-original-colour-new-teak-hardwood-/220929631080
                    [itemCategoryId] => 4621162015
                    [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=4621162015
                )

        )

    [8389242015] => Array
        (
            [0] => Array
                (
                    [itemId] => 222388502715
                    [itemName] => New Folding Round Teak Picnic Table Wood Garden Premium 50cm Foldup Solid Sale
                    [itemPrice] => 33.33
                    [itemCurrencySign] => £
                    [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/bWcAAOSw4DJYiIOA/$_57.JPG?set_id=8800005007
                    [itemImages] => Array
                        (
                            [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/bWcAAOSw4DJYiIOA/$_57.JPG?set_id=8800005007
                            [1] => http://i.ebayimg.com/00/s/OTg1WDEwMDA=/z/kAoAAOSwo4pYiIOH/$_57.JPG?set_id=8800005007
                        )

                    [itemQuantity] => 50
                    [itemStartTime] => 2017-01-25T12:00:13.000Z
                    [itemEndTime] => 2017-02-24T12:00:13.000Z
                    [itemUrl] => http://www.ebay.com/itm/New-Folding-Round-Teak-Picnic-Table-Wood-Garden-Premium-50cm-Foldup-Solid-Sale-/222388502715
                    [itemCategoryId] => 8389242015
                    [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=8389242015
                )

        )
)    

我希望在多维数组中存在的所有项目中基于itemStartTime获取最新的两个项目,以便最终数组看起来如下所示

Array
(
    [0] => Array
        (
            [itemId] => 222388502715
            [itemName] => New Folding Round Teak Picnic Table Wood Garden Premium 50cm Foldup Solid Sale
            [itemPrice] => 33.33
            [itemCurrencySign] => £
            [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/bWcAAOSw4DJYiIOA/$_57.JPG?set_id=8800005007
            [itemImages] => Array
                (
                    [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/bWcAAOSw4DJYiIOA/$_57.JPG?set_id=8800005007
                    [1] => http://i.ebayimg.com/00/s/OTg1WDEwMDA=/z/kAoAAOSwo4pYiIOH/$_57.JPG?set_id=8800005007
                )

            [itemQuantity] => 50
            [itemStartTime] => 2017-01-25T12:00:13.000Z
            [itemEndTime] => 2017-02-24T12:00:13.000Z
            [itemUrl] => http://www.ebay.com/itm/New-Folding-Round-Teak-Picnic-Table-Wood-Garden-Premium-50cm-Foldup-Solid-Sale-/222388502715
            [itemCategoryId] => 8389242015
            [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=8389242015
        )

        [1] => Array
                (
                    [itemId] => 220929631080
                    [itemName] => Teak Cleaner - 1ltr bring back the original colour of new teak/hardwood!
                    [itemPrice] => 19.5
                    [itemCurrencySign] => £
                    [itemGalleryUrl] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F
                    [itemImages] => Array
                        (
                            [0] => http://i.ebayimg.com/00/s/NjU1WDgwMA==/z/HWIAAOSw8vZXMKGK/$_1.JPG?set_id=880000500F
                            [1] => http://i.ebayimg.com/00/s/MTEzMVgxNjAw/z/u8kAAOSw3mpXMKGT/$_1.JPG?set_id=880000500F
                        )

                    [itemQuantity] => 204
                    [itemStartTime] => 2012-01-10T16:59:50.000Z
                    [itemEndTime] => 2017-02-12T16:59:50.000Z
                    [itemUrl] => http://www.ebay.com/itm/Teak-Cleaner-1ltr-bring-back-original-colour-new-teak-hardwood-/220929631080
                    [itemCategoryId] => 4621162015
                    [itemCategoryUrl] => http://stores.ebay.co.uk/JATI-Teak-and-Woven-Furniture/_i.html?_fsub=4621162015
                )



)

TBH我完全迷失在这个没有线索,然后解析每个输出和使用foreach但这对于更大的阵列来说太慢了

对此的任何帮助都会非常棒!

到目前为止我的解决方案是重新排列每个数组并准备一个新的数组,只保存项目,然后按照这样对它们进行排序

foreach ($storeItemsArray as $key => $value)
                foreach ($value as $key_sub => $value_sub)
                    $itemsArray[]=$value_sub;



            // sort items and get the latest first
            usort($itemsArray, function($a, $b) {
                      $ad = new DateTime($b['itemStartTime']);
                      $bd = new DateTime($a['itemStartTime']);

                      if ($ad == $bd) {
                        return 0;
                      }

                    return $ad < $bd ? -1 : 1;
            });

更好的东西然后让我抬头我只是担心对于持有400或500件物品的阵列会很慢:(

3 个答案:

答案 0 :(得分:1)

这在技术上不使用foreach()并且可能有用。我所做的就是删除第一维,以便使用call_user_func_array() array_merge()轻松排序。

$newArr = call_user_func_array('array_merge', $arr); //To remove 1st Dimension

usort($newArr, function($x, $y){

    $x = strtotime($x['itemStartTime']);
    $y = strtotime($y['itemStartTime']);

    if($x>$y){

        return 0; 
    }

    return 1;
});

var_dump(array_slice($newArr, 2));

输出

array(2) {
    [0] => array(3) {
        ["itemId"] => int(220929626118)["itemName"] => string(80)
        "Teak Patinizer - 1ltr patinizer to maintain the silver-patina or weathered look!" ["itemStartTime"] => string(24)
        "2012-01-10T16:49:41.000Z"
    }[1] => array(3) {
        ["itemId"] => int(220929621668)["itemName"] => string(78)
        "Teak Protector - 1ltr water-based teak wood protector - maintain honey colour!" ["itemStartTime"] => string(24)
        "2012-01-10T16:40:03.000Z"
    }
}

答案 1 :(得分:0)

我知道你提到foreach对大型阵列来说太慢了 - 但除非你真的处理大量数据,否则这仍然是我所采取的道路。这主要取决于Array的结构。

我首先将它们全部合并到一个数组中,然后执行排序并从排序后的数组中拉出前2项。

我将假定(基于数据)这些项目最初被分组到类别中。

// $categoryItems = array(...) // This is your original array.
$items = array() // An empty array for our items

// Combine all items into one array.
foreach($categoryItems as $category) {
    foreach($category as $item) {
        $items[] = $item;
    }
}

// Sort items using a custom sort function
$sortedItems = usort($items, "sortItems");

// This variable will contain your 2 items
$latestItems = array_slice($sortedItems, 0, 2);

function sortItems($a, $b) {
    if ($a['itemStartTime'] == $b['itemStartTime']) {
        return 0;
    }
    return ($a['itemStartTime'] < $b['itemStartTime']) ? -1 : 1;
}

这是未经测试的代码 - 所以可能需要一些小的调整

答案 2 :(得分:0)

我想这是您可能正在寻找的代码。我编写了一个用户定义的函数来帮助您提取您正在寻找的更高日期。您可以在函数的第二个参数中指定所需较高日期的长度。如果您需要解释,请在评论中告诉我。

<?php

    function extract_top_dates($arr, $out_arr_len) {
        $dates = array();
        foreach($arr as $key1 => $value1) {
            foreach($value1 as $key2 => $value2) {
                $dates[$value2['itemId']] = strtotime($value2['itemStartTime']);
            }
        }

        arsort($dates);
        $top_dates = array_slice($dates, 0, $out_arr_len, true);
        $arr_new = array();

        foreach($arr as $key1 => $value1) {
            foreach($value1 as $key2 => $value2) {
                if(in_array($value2['itemId'], array_keys($top_dates))) {
                    $arr_new[] = $value2;
                }
            }
        }

        rsort($arr_new);

        return $arr_new;
    }

    var_dump(extract_top_dates($arr, 2));

?>

希望它有所帮助!