doubo1711 2013-10-10 07:17
浏览 92

PHP if语句,用于检查包含数组的列中是否存在值

I have a field that stores multiple zip codes. A query result for the zip codes column may contain several zip codes: 90027,90028,90068

I need an if statement to check if a single zip code is in the result

$zipstring = $rows['pool_zip_codes']);

$zipsql = "SELECT `pool_zip_codes` FROM `cases` WHERE `id` = '{$rowid}' AND `pool_zip_codes` IN ('{$zipstring}') ";

$zipqry = mysql_query($zipsql);
$zipresult = mysql_fetch_row($zipqry);

if (($zipresult[0]) == '90068') { 
this zip code is in the list
} else {
not in list
}
};
  • 写回答

4条回答 默认 最新

  • douliaotong4944 2013-10-10 07:20
    关注

    If I read your question correctly, you want to distinguish between

    #####
    

    and

    #####,#####,#####...
    

    To do this, just use a regex to check if the field matches 5 digits.

    if (preg_match("/^\d{5}$/", $zipresult[0])) {
        ...
    }
    

    Otherwise, as the others are saying, use in_array(). What they're not saying is that you'd have to explode() the string first, to make an array:

    $exploded = explode(",", $zipresult[0]);
    
    if (in_array($exploded, "99999")) {
        ....
    }
    

    EDIT per your comment you could use strpos()

    $targetcode = "99999";
    $found = array();
    foreach ($zipresult as $row) {
        if (strpos($row['pool_zip_codes'], $targetcode) !== false) {
            $found[] = $row;
        }   
    }
    

    or in_array()

    $targetcode = "99999";
    $found = array();
    foreach ($zipresult as $row) {
        $exploded = explode(",", $row['pool_zip_codes']);
        if (in_array($exploded, $targetcode)) {
           $found[] = $row;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab