简体   繁体   English

PHP数组搜索 - key => string

[英]PHP Array Search - key => string

I got some trouble with in_array() 我在使用in_array()遇到了一些麻烦

$list = array(
    "files/" => "/system/application/files/_index.php",
    "misc/chat/" => "/system/application/misc/chat/_index.php"
);

I have this $_GET['f'] which holds the string files/ . 我有这个$_GET['f'] ,它包含字符串files/
How can I search through the array for possible matches? 如何在数组中搜索可能的匹配?

If the string is found in the array, then the file should be included 如果在数组中找到该字符串,则应包含该文件

Thanks for any help :) 谢谢你的帮助 :)

It's really simple. 这很简单。 All you need to do is check if the array element is set. 您需要做的就是检查数组元素是否已设置。 The language construct that's usually used is isset() (yes, it's that obvious)... 通常使用的语言结构是isset() (是的,很明显)...

if (isset($list[$_GET['f']])) {
}

There's no need to call a function for this, isset is cleaner and easier to read (IMHO)... 没有必要为此调用函数, isset更清晰,更易于阅读(恕我直言)...

Note that isset is not actually a function. 请注意, isset实际上不是一个函数。 It's a language construct. 这是一种语言结构。 That has a few implications: 这有一些影响:

  1. You can't use isset on the return from a function ( isset(foo()) won't work). 你不能在函数返回时使用issetisset(foo())将无效)。 It will only work on a variable (or a composition of variables such as array accessing or object accessing). 它只适用于变量(或变量组合,如数组访问或对象访问)。

  2. It doesn't have the overhead of a function call, so it's always fast. 它没有函数调用的开销,所以它总是很快。 The overall overhead of a function call is a micro-optimization to worry about, but it's worth mentioning if you're in a tight loop, it can add up. 函数调用的总体开销是一个需要担心的微优化,但值得一提的是,如果你处于紧密循环中,它可以加起来。

  3. You can't call isset as a variable function. 您不能将isset称为变量函数。 This won't work: 这不起作用:

     $func = 'isset'; $func($var); 

array_key_exists is a function that returns true of the supplied key is in the array. array_key_exists是一个函数,它返回数组中提供的键的true。

if(array_key_exists( $_GET['f'], $list )) {
    echo $list[$_GET['f']];
}

You can use in_array() in conjunction with array_keys() : 您可以将in_array()array_keys()结合使用:

if (in_array($_GET['f'], array_keys($list))) {
  // it's in the array
}

array_keys() returns an array of the keys from its input array. array_keys()从其输入数组返回键的数组。 Using $list as input, it would produce: 使用$list作为输入,它将产生:

array("files/", "misc/chat/");

Then you use in_array() to search the output from array_keys() . 然后使用in_array()array_keys()搜索输出。

Use array_key_exists . 使用array_key_exists

if(array_key_exists($_GET['f'], $list)){
  // Do something with $list[$_GET['f']];
}

in_array() searches array for key using loose comparison unless strict is set. in_array()使用松散比较在数组中搜索key,除非设置了strict。 it's like below. 就像下面一样。

foreach ($array as $value){
    if ($key == $value){
        return true;
    }
}

My way. 我的方式。

function include_in_array($key, $array)
{
    foreach($array as $value){
        if ( strpos($value, $key) !== false ){
            return false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM