total noob here. I’ve an array which looks like:
Array
(
[15] => Array
(
[0] => Array
(
[id] => 5216
[name] => Grow your own Irish Shamrock gifts set
[ordered] => 473
[image] => FunShamrockPot.jpg
)
[1] => Array
(
[id] => 5217
[name] => Irish Shamrock Seeds
[ordered] => 357
[image] => FunShamrockSeed.jpg
)
[2] => Array
(
[id] => 5759
[name] => Ireland Fleece with Shamrock Badge
[ordered] => 1
[image] => IrelandFleeceShamrocks.jpg
)
)
[31] => Array
(
[0] => Array
(
[id] => 5081
[name] => Tartan Guinness Flat Cap
[ordered] => 356
[image] => GuinnessTartanFlatCap.jpg
)
[1] => Array
(
[id] => 4894
[name] => Black signature emblem Guinness baseball hat
[ordered] => 176
[image] => GuinnessSigBBHat.jpg
)
The array is called $products and I’m trying to just print out the first five arrays in each top array (if that makes sense) in other words, array[15]<– first five arrays under 15, array[31]<– first 5 arrays in here.
the code I’m trying is:
foreach($products as $key=>$value) {
array_slice($value, 0, 5);
foreach($value as $product) {
echo $product['name'] . '<br/>';
}
echo '<br/>';
}
I can’t figure out why array_slice won’t work, what am I doing wrong?
The problem is that
array_slicedoes not modify the input array, you need the return value: