views:

79

answers:

6

If I had this code:

$result = 1;

$square = array(

"00" => -1,
"0" => 0,
"1" => 1,

);

And wanted to know whether $result is equal to ANY of the array VALUES of $square (-1, 0 or 1).

I´m am guessing there should be a function that compares a variable to all the array´s values and returs TRUE or FALSE accordingly.

If there isn´t such a function I am open to any suggestions and/or workarounds you might have hidden under your sleeves :)

Thanks in advance

A: 
var_dump(in_array($result, $square));
zerkms
Trying it out, thanks!
Trufa
+1  A: 

in_array should work for you.

if(in_array($result, $square)) {
   //$result is in there.
}
Jacob Relkin
Trying it out, thanks!
Trufa
For the record, this function is SUPER SLOW for large arrays.
mattbasta
@mattbasta: is there any alternative?
zerkms
@mattbasta I dont really care for speed just now, but thanks for the clarification!
Trufa
A: 

if(array_search($result,$square)===false) { echo "DNE"; }

Note the ===!

EDIT: switched function parameters - sorry

Steve
@meagar, he's not using `in_array`.
Matthew Flaschen
Looks like `in_array` is briefer though.
Steve
+2  A: 

You're looking for in_array():

$result = 1;
$square = array( "00" => -1, "0" => 0, "1" => 1, );

if (in_array($result, $square)) {
    echo "Found something!";
}
David Titarenco
Worked like a charm. Thank you very much!
Trufa
A: 

There is a function in php that will do this. It is known as array_search(); For your code above you would use

if(false !== array_search($square, $result)){
echo "Found something";
}

The link to the documentation can be found here. Look towards the bottom for code examples.

Slruh
You need to compare with `===` to avoid problems if the [0] value is returned.
Steve
Example contains a serious flaw, and will fail if the index of the element you're searching for evaluates to `false` when coerced to a bool. You must use `if (false !== array_search(...))`.
meagar
Thank you for noticing...I fixed it in the example above
Slruh
+1  A: 

If your array is going to be large (>500 elements), you're going to want to do this:

$flip_square = array_flip($square);
return isset($flip_square["string_to_search_for"]);

If you don't do this, it can be deathly slow. This is many times faster than in_array().

mattbasta
Use `array_key_exists` instead of `isset`, which will return false in the weird corner case where the value you're searching for originally existed at index `null`.
meagar
Just an FYI if the OP plans to use this.. it will not work if any values are (or will ever be) anything BUT integers or strings.
David Titarenco
@David Titarenco: to be precise: anything but `scalar`
zerkms
@mattbasta: comment this please: http://ideone.com/AomXC
zerkms
@zerkms: Sorry, but you're wrong. You can't flip floats or boolean values, both of which are scalars: `array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in test.php on line 3`
David Titarenco
@David Titarenco: yup, i was wrong.
zerkms
@zerkms It depends on context; if you're going to search an array once, `in_array` is faster than flipping the array. If you're going to search a large array many times, `array_flip` is faster. Neither solution is superior in all cases, but all things being equal you should choose the purpose-built function: `in_array`.
meagar
@meagar: you said that `in_array()` is deathly slow. well, it doesn't ;-) change my sample so it becomes **really** slower?
zerkms
@zerkms No I didn't, I'm agreeing with you. And I've [updated your sample](http://ideone.com/tenBw) so that `array_flip` is an order of magnitude faster than `in_array`. As I said, it's about context.
meagar
Well, i've found that about 70 iterations of `in_array` becomes slower that `isset` in my sample. This is not applicable to real life anyway...
zerkms
@meagar: yes, show me sample in real life when we need to make 1000 array lookups ;-)
zerkms
Thanks mattbasta, I´ll give it a go when the situation calls for it and se how it works out!
Trufa
@zerkms It's not about doing lots of lookups; [2 lookups](http://ideone.com/SYhKS) in a sufficiently large array could be slower with `in_array` than `array_flip`.
meagar
100 000 elements in php array :-S this is much more than 500.
zerkms
@zerkms As @meagar showed, you would only perform `array_flip` once. This would take place after the instantiation of the main array. Otherwise, you're wasting CPU cycles doing useless work. Also, my response was based on what I'm anticipating the OP is likely intending to do (namely, lots of lookups).
mattbasta
Yes. And he has an array much fewer than 100k elements in it. So your advice is useless.
zerkms
@zerkms Apologies if my code has offended you, but the technique that I've presented is standard practice. Even the comments on the PHP docs page indicate this. http://bit.ly/b3FVIi
mattbasta
@mattbasta:comments in php manual are written mostly by newbies, it is a fact. Meanwhile - don't you agree that your advice is useless for array of 10-50 elements and < 100 lookups through that array?
zerkms
@zerkms: No, I don't agree. Frankly, this technique *isn't* useless. This is the proper means of performing lookups on an array. `in_array()` certainly does a decent job, but it is a better habit to use `isset()`. Regardless, if you're going to argue over hundredths of a second, the fine. You win. Congratulations.
mattbasta
@mattbasta: this technique is meaningful only for huge array and only for a lot of lookups. My tests proved it. It is sensless to argue with the numbers.
zerkms