dongxia527680 2012-04-05 06:59
浏览 8
已采纳

PHP,检查My-Sql数据中的值

$result = $this->db->query($query)->result_array();


        if($result[9]['b_code'] != FALSE)
            {

        $result_array = array(
                            '0' => $result[0]['b_code'],
                            '1' => $result[1]['b_code'],
                            '2' => $result[2]['b_code'],
                            '3' => $result[3]['b_code'],
                            '4' => $result[4]['b_code'],
                            '5' => $result[5]['b_code'],
                            '6' => $result[6]['b_code'],
                            '7' => $result[7]['b_code'],
                            '8' => $result[8]['b_code'],
                            '9' => $result[9]['b_code']
                        );


                return $result_array;
            }
        else
            return FALSE;

It extracts 10 data in rows and make it a new array as $result_array;

Sometimes, it won't have 10 results and may extract 5 or 8.

So, I wanna check wether there is a value for last one, which is $result[9]['b_code] or not.

Should I use isset?

  • 写回答

5条回答 默认 最新

  • duanhu7400 2012-04-05 07:17
    关注

    Should I use isset?

    Yes, isset­Docs is the right language construct if you want to test if a value is set (e.g. set and not NULL­Docs).

    In your particular case you can also check the length of $result, that is done with count­Docs:

    $numberOfElements = count($result);
    

    Some example code with isset:

    if (!isset($result[9]))
    {
        return FALSE;
    }
    
    return array_map(function($v) {return $v['b_code'];}, $result);
    

    Note that in this example we're preventing to repeat ourselves 10 times only to copy each b_code element. Instead, this is done with array mapping, see array_map­Docs.

    A probably more readable variant, this time with count:

    if (10 != count($result))
    {
        return FALSE;
    }
    
    $resultArray = array();
    foreach ($result as $v)
    {
        $resultArray[] = $v['b_code'];
    }
    return $resultArray;
    

    This variant is making use of foreach­Docs to prevent assigning 10 elements one by one.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?