dssu33392 2018-09-15 09:10
浏览 8
已采纳

PHP数组找到两个数字是一个接一个

I have a sorted array with these stop_ids.

1, 6, 13, 18, 31,

I just want to find given first search value(6) is before the second given value(31). I tried something like this. That means the find order should be, first (6) then (13) not (13) first and (6) then.

foreach ($parent_array as $key => $value) {
    $k = $key;
    sort($routes); //another array with above values(stop_ids)
    $st = 0;
    foreach ($routes as $key => $value) {
       if($st == 1){
         unset($parent_array[$k]);
         break;    
       }
       elseif($value->stop_id == 31){
         $st = 1;
         continue;
       }
    }
}
return $parent_array;

I can provide two values. Here I used second value(31) only. Any help ???

  • 写回答

1条回答 默认 最新

  • douxian3170 2018-09-15 09:17
    关注

    Get array keys, under which is every number is located and compare this keys:

    function firstNumberFirst($array, $first_number, $second_number) 
    {
        return array_search($first_number, $array) < array_search($second_number, $array);
    }
    
    $a = [1, 6, 13, 18, 31];     
    var_dump(
        firstNumberFirst($a, 6, 13),
        firstNumberFirst($a, 6, 18),
        firstNumberFirst($a, 13, 6)
    );
    

    If array is not zero-indexed - apply array_values first.

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

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据