douyao4632 2016-11-20 23:11
浏览 15
已采纳

数组 - 检查数组是否两次选择相同的值

I'm working on a script which rotates multiple slogans from an array. It picks a random array item and displays it, like so:

function sloganrotator_sc() {
    global $sloganrotator_options;

    $sentences = $sloganrotator_options['slogans'];
    $array = explode("
", $sentences);

    if (count($array) > 1) {
        $randomize = array_rand($array);
        $result = $array[$randomize];

        return $result;
    } else {
        return $sloganrotator_options['slogans'];
    }
}
add_shortcode( 'slogan-rotator', 'sloganrotator_sc' );

What I'm trying to archieve is to check if the value on page 1 is not the same as on page 2.

For example: Before the page refresh, the value was 'Hello', after the refresh the value is 'Dogs'. I want to check if it doesn't pick the same item twice.

Something like this:

if($array[$randomize] == $result) {
    return 'Hold on, this one has already been shown!';
}

Sorry for my bad explanation, it's hard to explain..

  • 写回答

1条回答 默认 最新

  • dprfe04886 2016-11-21 02:19
    关注

    change this :

    if($array[$randomize] == $result) {
        return 'Hold on, this one has already been shown!';
    }
    

    to this :

    if(isset($_SESSION['check']) AND !empty($_SESSION['check']) AND $_SESSION['check'] == $result){
        return 'Hold on, this one has already been shown!';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号