检查这样的php数组中是否存在值? [英] Check if a value exists in a php array like this?

查看:60
本文介绍了检查这样的php数组中是否存在值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个类似于以下数组的数组,如何在不使用循环等的情况下检查该查询字符串中是否存在来自该字符串的值?

Given an array like the one below, how could i check if an value that came in from a query string existed in that array, without using loops etc.?

我厌倦了使用 in_array ,但是即使值不存在,这似乎也返回了真。

I tired using in_array, but that seems to return true even if the value weren't there.

I需要检查该值是否在别名部分中。

I need to check if the value exists in the alias part.

我在下面发布的同一内容的键盘代码。

<?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;
}
?>


推荐答案

由于您使用的是多维数组,并且您要查找的值未设置为键,您将需要在此处使用循环。但是,如果您真的想避免循环,可以采取一些方法。

Since you're using a multi-dimensional array and the value you're looking for isn't set as a key, you're going to need to use a loop here. However, if you really want to avoid a loop, you could take a few approaches.

第一个也是可能最有效的方法是存储您在另一个数组中查找的别名,但作为该数组的键:

The first and probably most efficient would be to store the aliases you're looking for in another array but as the key of that array:

$aliases = array(
    'mediterranean-cuisine' => 1,
    'asian-cuisine' => 1,
    'greek-cuisine' => 1
);

然后您可以使用 array_key_exists() isset() 来确定您要查找的值是否存在:

Then you can use either array_key_exists() or isset() to determine if the value you're looking for is there:

if (isset($aliases[$_GET['alias']])) {
    echo 'Found!';
}

由于数组是PHP中的哈希映射,因此不会使用实际的

Because arrays are hash-maps in PHP, this won't use an actual loop internally and should be more efficient than any loop-based approach.

此方法的另一种选择是利用数组回调/步行功能,例如 array_filter()

An alternative to this could make use of an array callback / walking function such as array_filter():

function alias_check($category) {
    return ($category['alias'] === $_GET['alias']);
}

$matches = array_filter($categories, 'alias_check');
if (count($matches) > 0) {
    echo 'Found!';
}

在内部,此确实遍历您的数组,因此从技术上讲它不会避免循环;

Internally, this does iterate over each element in your array so it technically doesn't avoid a loop; however, you won't have to write one yourself which is nice.

不过,最直接,最简洁的方法是使用一个简单的循环:

Though, the most straightforward and cleanest approach would be to use a simple loop:

foreach ($categories as $category) {
    if ($category['alias'] === $_GET['alias']) {
        echo 'Found';
        break;
    }
}

这篇关于检查这样的php数组中是否存在值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆