-2

My switch case allows me to find the values which match one of a list of values

I need to extend this code/logic so that I can collect all consecutive keys starting from the first occurring C or D until the element before the second occurring C or D.

Here is what I would like to do:

foreach ($array as $key => $value) {
    switch($value) {
        case "C": 
        case "D":
        // Store all keys 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"

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Marie
  • 13
  • 4
  • 4
    can't you write simple if else logic in your switch case? – Ahad Dec 29 '16 at 06:32
  • 1
    Just take `$key` value – anon Dec 29 '16 at 06:33
  • 1
    not sure with what do you mean with *Take all key from "C" until I come across a value "C" or "D"*, anyway, for dirty trick, you could always use [goto in php](http://php.net/manual/en/control-structures.goto.php) but well, yeah, it'll feel dirty. and i'll stick with @xFighter approach, simple `if`s. – Bagus Tesa Dec 29 '16 at 06:36
  • 1
    `all key from "C" until I come across a value "C" or "D"` <-- with that logic isn't the result just `87` as the next item is `[19] => D`? – bansi Dec 29 '16 at 06:36
  • are you getting any error – Ahad Dec 29 '16 at 06:39
  • Wait, have I understood the OP correctly? He says take all values until he sees "C", "D", I think my answers are wrong – Lionel Chan Dec 29 '16 at 07:01
  • yeah it also seems confusing to me. but what she wants is the output should look like how she mentioned above. – Ahad Dec 29 '16 at 07:07
  • it also means that the output should be unique. what do you think ? @ Lionel Chan – Ahad Dec 29 '16 at 07:08
  • Why it doesn't stop at 87? Since you say "until it sees D". Isn't it stopped at 87? – Lionel Chan Dec 29 '16 at 07:14
  • @Lionel Chan "C" and "D" are initial values, the keys are taken into account until a second C or a second D intervenes in the values, not before. – Marie Dec 29 '16 at 07:18

3 Answers3

1

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
)
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69
0

first thing to point out to your code is that you associative array's syntax has error. and then you can use the way i shown in the below

<?php

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


 foreach ($array as $key => $value) {
    switch($value) {
        case "C":
        case "D":
        case "e":
            echo $key . ' ';
        break;
    }
}

Result : 87 19 101 22

as my answer gives you another extra outputs then you can only take the first three items or you can write duplication filter script as shown in Lionel Chan's answer.

Ahad
  • 674
  • 1
  • 9
  • 22
  • I may be wrong. I just realize OP specified a weird logic there.. :p – Lionel Chan Dec 29 '16 at 07:03
  • @xFighter Thank you for your reply. However, after verifications, I do not think my syntax is false if I refer to this: http://stackoverflow.com/a/4163212 – Marie Dec 29 '16 at 07:25
  • *Also, I would like to create a loop that takes all keys until a second C or D value appears, and not take ready-defined keys. – Marie Dec 29 '16 at 07:41
0
  • Build a lookup array of the qualifying values. Key searches are more efficient than value searches because of how PHP treats arrays like hashmaps.
  • Once either of the qualifying values is encountered more than once, break the loop.
  • When the lookup array has a non-zero sum, begin pushing data into the result array.

Code: (Demo)

$whitelist = array_fill_keys(['C', 'D'], 0);
$result = [];
foreach ($array as $k => $v) {
    if (isset($whitelist[$v])) {
        if ($whitelist[$v]) {
            break;
        }
        ++$whitelist[$v];
    }
    if (array_sum($whitelist)) {
        $result[] = $k;
    }
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136