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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?