drngnh708353 2014-04-13 08:15
浏览 143
已采纳

在数组中查找字符串

My problem is basically, I will be trying to get data from a table called, 'rank' and it will have data formatted like, "1,2,3,4,5" etc to grant permissions. So Basically I am trying to make it an array and find if one number is there in the array. Basically making it an array is not working. How would I get this done? Here is my code below:

<?php
    function rankCheck($rank) {
        $ranks = "1,2,3,4,5";
        print_r($uRanks = array($ranks));
        if(in_array($rank, $uRanks)) {
            return true;
        } else {
            return false;
        }
    }
    if(rankCheck(5) == true) { echo "Hello"; } else { echo "What?"; }
?>

This code returns false, while it should return true. This is just a basic algorithm.

The print_r Display:

Array ( [0] => 1,2,3,4,5 )
  • 写回答

4条回答 默认 最新

  • dtf579777 2014-04-13 08:19
    关注

    Problem solved. I used the explode function instead like this:

    <?php
        function rankCheck($rank) {
            $ranks = "1,2,3,4,5";
            print_r($uRanks = explode(',', $ranks));
            if(in_array($rank, $uRanks)) {
                return true;
            } else {
                return false;
            }
        }
        if(rankCheck(5) == true) { echo "Hello"; } else { echo "What?"; }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式