如果遇到条件,从多维数组中获取随机数组值

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.

Then you need to pull out one random treadmill entirely, and not individual attributes.

foreach( $treads as $brand => $defaulttread2 ) {
    if($treads[$brand]['discontinued'] != '1') {
        return $defaulttread2;
    }
}

Then call the function once, and echo the attributes individually after that:

$treadmill = treadDetails();
echo $treadmill['brand'];
echo $treadmill['description'];
echo $treadmill['image'];

Of course you probably want to rename your function now, and remove the $a argument.

If it were me, I'd rewrite the method to be something like this:

function randomTreadmill() {
    global $treadmills;

    // Filter out discontinued treadmills
    $filtered = array_filter($treadmills, function($treadmill) {
        return $treadmill['discontinued'] == 0;
    });

    // Shuffle, and return the first one
    shuffle($filtered);
    return array_shift($filtered);
}

Working example: http://3v4l.org/mkcuK

If you need a random entry from that array try this:

$randomKey = array_rand($treads, 1);

echo $treads[$randomKey]['brand']; 

Not entirely sure if this is what you're after but from my understanding this is what you want.

 function treadDetails() {
        global $treadmills;
        $treads = $treadmills;
        shuffle($treads);
        foreach( $treads as $tread ) {
            if($tread['discontinued'] != '1') {
                return $tread;
            }
        }
    }

product = treadDetails();

echo product['brand'];
echo product['description'];
echo product['image'];