doubang9906 2016-12-29 06:30
浏览 38
已采纳

如何在PHP中使用特定值的键?

My switch case allows to find the tables having two specific values. Here is what I would like to do:

foreach ($array as $key => $value) {
   switch($value) {
             case "C": 
             case "D":
     //Take all key from "C" until I come across a second value "C" or "D"
   }
}

Here is an example :

$array =
 (
      [53] => q
      [61] => f
      [74] => s
      [87] => C
      [19] => D
      [101] => e
      [22] => C
      [13] => h
 )

Result : "87 19 101 "

  • 写回答

2条回答 默认 最新

  • douci1541 2016-12-29 07:41
    关注

    This is a weird logic, but this should work:

    //Triggers are the flags that trigger opening or close of filters
    $triggers = array('C', 'D');
    $filtered = [];
    $stack = [];
    
    foreach($array as $k => $v) {
    
        //If the trigger is opened and I see it again, break the loop
        if (in_array($v, $stack)) {
            break;
        }
    
        //If we sees the trigger OR the stack is not empty (we are opened)
        //continuously pull the keys out
        if (in_array($v, $triggers) || !empty($stack)) {
            $stack[] = $v;
            $filtered[] = $k;
        }
    }
    

    The output for this is

    Array
    (
      [0] => 87
      [1] => 19
      [2] => 101
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页