2

I got some trouble with 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/.
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 :)

5 Answers 5

6

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)...

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

There's no need to call a function for this, isset is cleaner and easier to read (IMHO)...

Note that isset is not actually a function. 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). 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. This won't work:

    $func = 'isset';
    $func($var);
    
Sign up to request clarification or add additional context in comments.

2 Comments

Somewhat counterintuitively, this will result in an error (or a notice, depending on the error reporting level) if there is no GET parameter called f.
Also, with longer variable names isset() can mask typos in the variable name, so array_key_exists()˙results in safer code. That said, I tend to be lazy and use isset() myself...
4

array_key_exists is a function that returns true of the supplied key is in the array.

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

3 Comments

+1 Eek, I never even knew of array_key_exists(). Better than my solution.
Only by a single function call :) besides, I think ircmaxwell got us both.
I wouldn't call either of these two "complex". In fact, there is a case where array_key_exists should be used over isset: when the array element value can be null and you want it to return true (isset would return false). But that's not the case here. My issue with AKE is that it's not trivial to realize which is the array unless you memorize parameter order or look it up. The isset call just looks like a normal array access, so it's easy to tell if it's switched at a glance...
2

You can use in_array() in conjunction with 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. Using $list as input, it would produce:

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

Then you use in_array() to search the output from array_keys().

Comments

1

Use array_key_exists.

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

Comments

0

in_array() searches array for key using loose comparison unless strict is set. 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;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.