I have the following array:
$historicalFullList = ['Fecha','H-kWH-Total','H-kWH-Pico','H-kWH-Resto','H-kWH-Valle','H-Acum-Pico','H-Acum-Resto','H-Acum-Valle','H-Fecha-Pico','H-Fecha-Resto','H-Fecha-Valle','H-kW-Pico','H-kW-Resto','H-kW-Valle','H-kVARH-Total','error','factMult','H-kW-Pico-C','H-kW-Resto-C','H-kW-Valle-C'];
I want to create a function to find if the substring 'H-k' exists in the array. (in this example array it should return TRUE)
For some reason the following code doesn't work. Is there a better way to do this?
function test($substring, $array){
foreach ($array as $item){
if (strpos($substring, $item)!== false){
return true;
}else{
return false;
}
}
}
then called it..
if (test('H-k',$historicalFullList)){
do stuff...
}
!==to!=test(...)function!==. strpos can return bothfalseand0with opposite meanings (falsemeans not found,0means found at first character). If you made the comparison!=, if the string started with the substring, it would be found and return0, which is a falsy value (can be coerced to false) so the type strict comparison is needed.