doutong1890 2014-08-02 18:14
浏览 75
已采纳

PHP比较两个值?

While comparing the values in the below array

array
  0 => 
    array
      0 => string '09:30' (length=5)
      1 => string '11:00' (length=7)
  1 => 
    array
      0 => string '11:01' (length=5)
      1 => string '18:00' (length=7)
  2 => 
    array
      0 => string '12:05' (length=5)
      1 => string '14:00' (length=7)
  3 => 
    array
      0 => string '14:00' (length=5)
      1 => string '20:20' (length=7)

If I am using if($a > $b) it is even giving true for equal values ($a = $b) // this is not comparision , while if($a - $b > 0) is giving accurate result.

Why so ?

EDIT: The code part where I am using cmp.

This woks fine

for($i=0; $i < $fr -1 ; $i++)
{
    if( $dtime[$i][1] - $dtime[$i+1][0] > 0 )
    {
        echo 'It is clashing';
        break;
    }
}

This does not works fine

for($i=0; $i < $fr -1 ; $i++)
{
    if( $dtime[$i][1] > $dtime[$i+1][0] )
    {
        echo 'It is clashing';
        break;
    }
}
  • 写回答

1条回答 默认 最新

  • dongliu6848 2014-08-02 18:33
    关注

    Though the way you're doing comparisons seems odd, I think you have a data cleaning problem. In the top part of your question it appears you're doing a var_dump(). Notice how'09:30' (length=5) is correct but '11:00' (length=7) is wrong because the length should be 5 also. There are some additional hidden characters (maybe or ) at the beginning or end of the data.

    To fix, try running $value = trim($value) on each of your array elements before comparing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化