php-in-array

Which is faster: in_array() or a bunch of expressions in PHP?

Is it faster to do the following: if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') { ... } Or: if (!in_array($var, array('test1', 'test2', 'test3', 'test4') { ... } Is there a number of values at which point it's faster to do one or the other? (In this case, the array used in the second option doesn'...

PHP in_array not working

Hey everyone, I am using the PHP in_array() function in order to authenticate (with sessions) if a user can access a particular page. For some reason, it is not working... PHP PAGE session_start(); require_once('../scripts/functions.php'); $role_auth = @$_SESSION['role_auth']; access($role_auth, array(0,1,2,3,4)); access FUNCTION ...

php array in_array oddity...

of all the languages i know im the weakest in php... I have a script... that takes a csv file and does some stuff with it... fairly simple. the issue i am having: in_array('username', $headers) ... returns null... while... print_r ($headers); shows username being the first entry in the csv. thoughts? mistakes i may have made? TIA ...

Problem with array

$modules = array( 'home' => 'home', 'login' => 'login', 'forum' => 'forum', 'topic' => 'topic', 'post' => 'post', 'profile' => 'profile', 'moderate' => 'moderate', 'search' => 'search', 'ucp' => 'usercp', 'ucc' => 'usercp', 'pm' => 'pm', 'members' => 'members', 'boardrule...

check existence of an object in array

Hi, at first I wanna say that I'm new in PHP. I have an implementation that checks an object is in array or not, if not adds another array. But it always returns false and adds in theorder array. How can I solve it? Here part of the code: $temp = new tempClass($x, $y); if (!in_array($temp, $temp_array)) { $temp2_array[] = $temp;...

in_array() - help on what specifically would return true

ANSWERED: THE FUNCTION WORKS AS I WANTED IT TO. I HAD A TYPO IN MY TEST PHP THAT MADE IT RETURN TRUE REGARDLESS. I am using in_array, and I'm trying to use it in a way where it will only return true if it's an exact match of one of the objects in the array, not just if it's "in" the array. e.g. 1. $sample_array = array('123', '234', '...

PHP in_array() can't even match a single character. Strict is set to true.

I've seen a million of these threads here already, and read through every single one. That, plus some serious Googling. UPDATE: I am rewriting this post to include complete code and explanation, so everyone understands what is going on and what I am trying to do. I am developing using CodeIgniter, so some syntax may look weird if you ...

in array sql query

I have the following inside a foreach loop (displaying my various videos), I'm trying to display some alternate text for the top three voted videos. What on earth am I doing wrong (a lot clearly)... $sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3"; $result = mysql_query($sql); $row = @mysql_fetch_...

in_array returning false when should return true

I've got a simple script that takes a word from a form and assesses whether it exists in a file (.txt). The txt file has a single word or phrase on each line. There are no \t's or \r in the file. However, when I submit the form, and POST the first word in the file (e.g. "the"), the following script returns false, when it should return ...

faster than in_array?

Dear all, I need to compare a value to a set of array. However, I need to compare multiple values in foreach. If using in_array, it can be slow, real slow. Is there any faster alternative? My current code is foreach($a as $b){ in_array($b, $array); } Thank you. ...