假设
所以,基本上,你想检查每个$array1键是否是$array的子字符串,是吗?
第一个问题(模式)
正则表达式模式/ [$ch] /可能不正确,因为它意味着:
literally match a single character present in the following list ($, c, h)
这种模式将匹配:
> hugo
>头盔
>队长
修正了模式
我想你想要的东西是$pattern =’/’. $ch. ‘/’ ;.
在这种情况下,var $ch得到扩展,因此对于$array1的第一个元素,它变为/ uit /.
这种模式意味着:
literally match the sequence uit
此模式将匹配:
> fruit1
> fruit4
不需要Preg_match
你可以使用strpos.
foreach($array1 as $needle => $val1){
if($val1 == 2) {
foreach ($array as $haystack => $val2) {
if (strpos($haystack, $needle) !== false) {
echo "$haystack => $val2\n";
}
}
}
}