skip to Main Content

I have a array like this

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
                )

        )
)    

and i want to get latest two items based on itemStartTime across all items that are present inside multidimensional array so final array will look something like below

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 i am totally lost on this one not getting clue other then parsing sorting each out and using foreach but that would be way too slow for bigger arrays

any help on this would be really awesome!

My solution till now is this rearranging each array and preparing a new array that will just hold items and then sort them like this

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

Something better then just give me a heads up i am just concerned it would be slow for arrays holding 400 or 500 items 🙁

3

Answers


  1. I know you mention foreach’s would be too slow for larger arrays – but unless you’re really dealing with massive amounts of data this is still the path I’d take. This is mainly down to the structure of your Array.

    I’d merge them all into one array first, then perform a sort and pull out the top 2 items from the sorted array.

    I’m going to presume (based on the data) that these items are initially grouped into categories.

    // $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;
    }
    

    This is untested code – so it may require some small tweaks

    Login or Signup to reply.
  2. I suppose this is the code you might be looking for. I’ve written a user-defined function to help you extract those higher dates you’re looking for. You can specify the length of the higher dates you want in the second parameter of the function. If you want the explanation, let me know in the comment.

    <?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));
    
    ?>
    

    Hope it helps!

    Login or Signup to reply.
  3. This is technically not using a foreach() and maybe of use. All I’ve done is remove the 1st dimension so you can easily sort by using call_user_func_array() with 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));
    

    Output:

    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"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search