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 "
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
)
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.