给定一个如下所示的数组,如何检查来自查询字符串的值是否存在于该数组中,而不使用循环等。?
我厌倦了使用in_array
,但即使价值不存在,这似乎也会返回。
我需要检查alias
部分中是否存在该值。
Codepad code of the same thing I posted below.
<?php
$categories = array (
'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
);
$category = 'asian-cuisine';
if(in_array($category,$categories)) {
echo 'A category with that name was not found! '.$category;
} else {
echo 'A category was found '.$category;
}
echo '<br>';
$category = 'xxx-cuisine';
if(in_array($category,$categories)) {
echo 'A category with that name was not found! '.$category;
} else {
echo 'A category was found '.$category;
}
?>
答案 0 :(得分:0)
您需要遍历数组并检查值是否存在,如此
$categories = array (
'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
);
$category = 'asian-cuisine';
$found = false;
foreach($categories as $cat) {
if($cat['alias'] == $category) {
echo 'A category was found '.$category;
$found = true;
break;
}
}
if(!$found) {
echo 'A category with that name was not found! '.$category;
}
答案 1 :(得分:0)
由于您使用的是多维数组,并且您要查找的值未设置为键,因此您需要在此处使用循环。但是,如果确实想要避免循环,您可以采取一些方法。
第一个也许最有效的方法是将你要查找的别名存储在另一个数组中,但作为该数组的键:
$aliases = array(
'mediterranean-cuisine' => 1,
'asian-cuisine' => 1,
'greek-cuisine' => 1
);
然后,您可以使用array_key_exists()
或isset()
来确定您要查找的值是否存在:
if (isset($aliases[$_GET['alias']])) {
echo 'Found!';
}
因为数组是PHP中的哈希映射,所以这不会在内部使用实际循环,并且应该比任何基于循环的方法更有效。
替代方法可以使用数组回调/步行功能,例如array_filter()
:
function alias_check($category) {
return ($category['alias'] === $_GET['alias']);
}
$matches = array_filter($categories, 'alias_check');
if (count($matches) > 0) {
echo 'Found!';
}
在内部,这个执行遍历数组中的每个元素,因此从技术上讲,它不会避免循环;但是,你不必自己写一个很好的。
尽管如此,最直接,最干净的方法是使用一个简单的循环:
foreach ($categories as $category) {
if ($category['alias'] === $_GET['alias']) {
echo 'Found';
break;
}
}
答案 2 :(得分:0)
在PHP&gt; = 5.5.0中,您可以使用array_column()
来获取别名数组。
$aliases = array_column($categories, 'alias');
然后您可以使用in_array()
:
$category = 'asian-cuisine';
if(in_array($category, $aliases)) {
echo 'A category was found '.$category;
} else {
echo 'A category with that name was not found! '.$category;
}
echo '<br>';
$category = 'xxx-cuisine';
if(in_array($category, $aliases)) {
echo 'A category was found '.$category;
} else {
echo 'A category with that name was not found! '.$category;
}
答案 3 :(得分:0)
使用&#34; Foreach&#34;导航到一个数组:
<?php
$categories = array (
'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
);
$category = 'asian-cuisine';
foreach($categories as $c => $k)
{
if(in_array($category,$k))
echo $category." founded in array ".$c."<br/>";
}
在PHP手册中查看有关Foreach的更多信息=&gt; http://php.net/manual/en/control-structures.foreach.php
编辑:不够快=)