1

I have a series of arrays and usually if i want to check if a value exists i iterate throu the array, use an if statement inside the foreach block then break or return, but recently I decided to use array_flip to flip the array then check if the key exists with isset:

<?php
    $arr = array(1, 0, 'yes', 'no', 'on', 'off' /* more keys */);

    /*
    foreach($arr as $value) {
        if ($value === 'on') { return 'xxx'; }
    }
    */

    //Alternative
    $arrFlipped = array_flip($arr);
    if (isset($arrFlipped['on'])) { return xxx; }
?>

The arrays are made from safe data, not user input, so the values would always be valid keys.

I'd like to know if this appoach is ok, what are the advantages and disadvantages?, wich one is faster or waste less resources? Sorry for my english... thanks!

Edit: OP asks for multiple values

2
  • Speed will depend on number of entries in the array; but if you're searching for an exact value, why not use in_array() or array_search()? Commented Sep 13, 2013 at 16:02
  • Yes, im aware of that now, but i failed to point out that i might be performing different actions for different values rather than return. Commented Sep 13, 2013 at 16:14

3 Answers 3

2

Don't.

First, it might look like an optimisation in your code, but the performance will not change: in the background, PHP will have to loop through the array anyway.

Second, don't be "smart". The more "tricks" you use in your code, the harder it is to maintain your code. 6 months from now, you'll be scratching your head wondering why the ** you decided to flip the array (you or whoever is tasked to maintain your code).

If you really want to implement something like that, at least encapsulate in a clearly named function so that you can see what you were going for:

function value_exists($arr, $value){
    $arrFlipped = array_flip($arr);
    if (isset($arrFlipped["$value"])) { return xxx; 
    }else{ return false; }
}

That way, when you write return value_exists($arr, $value); what you're doing is obvious, even if your implementation is unusual.

Third, you can use in_array or array_search to do what you want.

Fourth, if you are looking for optimizations and you aren't sure if it makes it better, just profile it. The easiest way is to use microtime:

$before = microtime(true);
// code to test
$now = microtime(true);
echo sprintf("Elapsed:  %f", $now-$before);

But you shouldn't be trying to optimize for performance unless you notice an actual problem.

Sign up to request clarification or add additional context in comments.

Comments

1

What about in_array?
return in_array('on', array(1, 0, 'yes', 'no', 'on', 'off'))
http://php.net/manual/en/function.in-array.php

1 Comment

great, but how about if i have to perform different actions for different values rather than return, should i use several if (in_array(...)) { do this }?
1

To check if multiple values exists in array , like you said in comment , use :

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Intersect the $target with the $arr and make sure the intersection is precisely equal to the number of elements in $target:

$arr = array(1, 0, 'yes', 'no', 'on', 'off' /* more keys */);

$target = array('yes', 'off');


if(count(array_intersect($arr, $target)) == count($target)){

    echo 'all values found in array' ;
}

This works also for one element , but it should be in array as well

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.