Reputation: 11
I would like to find a way to add a condition in my "if" related to a first condition. Is there a way translating "FOLLOW BY" in php?
My start script with one condition:
foreach ($array as $key => $value) {
if ($begin == false && $value == 'T') {
...
}}
My final script with two conditions related :
foreach ($array as $key => $value) {
if ($begin == false && $value == 'T' FOLLOW BY $value == 'D') {
...
}}
Array for example :
Word => S
Word => H
Word => T //related with D
Word => D //related with T
Word => Q
Upvotes: 1
Views: 41
Reputation: 635
I don't know if my answer fit your case, but a simpler solution would be to just implode the array and perform a simple text search.
Upvotes: 0
Reputation: 62556
In case your $array
is numeric, you can check the value of $array[$key+1]
foreach ($array as $key => $value) {
if ($begin == false && $value == 'T' && array_key_exists($key+1, $array) && $array[$key+1] == 'D') {
...
}
}
Note that I also insert in the code a check if the next key exists in your array (otherwise you will have a problem in the last loop)
If your $array
is associative array it's a bit harder. We need to save the keys
in a temp array so we can check their position:
$array_keys = array_keys($array);
foreach ($array as $key => $value) {
$key_pos = array_search($key, $array_keys);
if ($begin == false && $value == 'T' && array_key_exists($key_pos+1, $array_keys) && $array[array_keys[$key_pos+1]] == 'D') {
...
}
}
Upvotes: 1