dongshi1207 2015-11-06 10:26
浏览 18
已采纳

比较基于数组先前值的数组

I have an array that looks like this:

Array ( 
    [0] => Array (  
        [START] => COI-COK 
        [RETURN] => CAI - DEL 
    ) 
    [1] => Array ( 
        [START] => COK - AMM 
        [RETURN] => CAI - DEL 
    ) 
)

I want to check if both 'start' and 'end' values of previous and current array are same or not. If not then, print some value. How can I do it?

This is my attempt:

foreach($data as $datas)
{     
    $old_start  = $datas['START'];
    $old_return = $datas['RETURN'];

    ...

    if( ($old_start == $datas['START']) && ($old_return == $datas['RETURN']))
    {

    }
    else
    {

    }
}

But it didn't work because all the time old_start value will be equal to $datas['START'].

print_r($data) shows this output:

Array ( 
    [0] => Array ( 
        [Sl] => 2 
        [TRAVELDAY] => 2015-11-11 
        [RETURNDAY] => 2015-11-27 
        [START] => COI-COK 
        [RETURN] => CAI - DEL 
    ) 
    [1] => Array ( 
        [Sl] => 1 
        [TRAVELDAY] => 2015-11-11 
        [RETURNDAY] => 2015-11-27 
        [START] => COK - AMM 
        [RETURN] => CAI - DEL 
    ) 
)
  • 写回答

4条回答 默认 最新

  • dougongyou7364 2015-11-06 10:30
    关注

    You have to put the assignment after the comparison, not before:

    $old_start = '';
    $old_return = '';
    foreach($data as $datas)
    {     
         //....
         if($old_start=='' || $old_start == $datas['START'] && $old_return == $datas['RETURN'])
         {
             //....
         }
         else
         {
             //code to be executed
         }
    
         $old_start = $datas['START'];
         $old_return = $datas['RETURN'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含