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
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.
This is untested code – so it may require some small tweaks
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.
Hope it helps!
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 usingcall_user_func_array()
witharray_merge()
.Output: