dsigg21445 2015-03-19 19:22
浏览 30
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • drd99007 2015-03-19 19:29
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题