I have an array like so:
$treadmills = [
'bowflexseries3' => [
'brand' => 'Bowflex',
'description' => 'Bowflex Series 3',
'image' => '/images/bowflex-series-3-150x150.jpg',
'discontinued' => '0'
],
'bowflexseries5' => [
'brand' => 'Bowflex',
'description' => 'Bowflex Series 5',
'image' => '/images/bowflex-series-5-treadmill-review-150x150.jpg',
'discontinued' => '1'
],
'bowflextc10' => [
'brand' => 'Bowflex',
'description' => 'Bowflex Treadclimber TC10',
'image' => '/images/bowflex-treadclimber-tc10-review-150x150.jpg',
'discontinued' => '0'
]
];
I'm trying to randomly pull out one treadmill and its details (brand, description, image) if discontinued = 0.
Here's what I got so far:
function treadDetails($a) {
global $treadmills;
$treads = $treadmills;
shuffle($treads);
foreach( $treads as $brand=>$defaulttread2 ) {
if($treads[$brand]['discontinued'] != '1') {
return $defaulttread2[$a];
}
}
}
echo treadDetails('brand');
echo treadDetails('description');
echo treadDetails('image');
The above doesn't quite work as it pulls details randomly from different treadmills. I dont want to mix details, so if it randomly chooses bowflextc10, then it should show "Bowflex Treadclimber TC10" and "/images/bowflex-treadclimber-tc10-review-150x150.jpg".
I tried array_rand instead of shuffle but it only pulls the first treadmill and details in the array every time.