It would be much easier to check the other way around. As if the key is in a string. Since the keys are unique, you have no duplicates.
$array = [ 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ] ]; $string = 'fruits[orange]'; $keys = array_keys($array['fruits']); foreach($keys as $fruit) { if(false !== stripos($string, $fruit)) { return true; } }
Although this solution is not necessarily perfect, the problem is not quite ordinary to begin with.
Andrew
source share