我有一个名为data.txt的平面文件。每行包含四个条目。如何检查数组中是否存在变量?
的data.txt
blue||green||purple||primary
green||yellow||blue||secondary
orange||red||yellow||secondary
purple||blue||red||secondary
yellow||red||blue||primary
red||orange||purple||primary
我试过这个,看看是否变“黄”的存在是为任何线的第一个条目:
$color = 'yellow';
$a = array('data.txt');
if (array_key_exists($color,$a)){
// If true
echo "$color Key exists!";
} else {
// If NOT true
echo "$color Key does not exist!";
}
,但它无法正常工作如预期。我可以改变什么来实现这个目标?谢谢....
虽然这会错误地匹配任何前缀换句话说,如果这个例子只是被设计出来的,即'$ search ='yell''会匹配'yellow'作为第一个字,那么需要改变正则表达式以确保分隔符跟随或线路终端 – Orbling 2011-05-19 00:34:11
@ Orbling:感谢那个忽略。更新。 – 2011-05-19 00:35:58