PHP - In_array Not Working?
Hi folks!
I have two arrays: 1) print_r($users1); Array ( => 1 [1] => 2609 [2] => 2612 [3] => 2620 [4] => 2621 [5] => 2624 [6] => 2627 [7] => 2629 ) 2) print_r($users2); Array ( => 1 [1] => 2609 [2] => 2610 [3] => 2611 [4] => 2612 [5] => 2617 [6] => 2618 [7] => 2619 [8] => 2620 [9] => 2621 [10] => 2624 [11] => 2625 [12] => 2626 [13] => 2627 [14] => 2628 [15] => 2629 ) now why does nothing return when I use in_array? if(in_array($users1,$users2)) return true; all values in $users1 is in $users2... Please help, need help fast! Thank you Similar TutorialsHello: I'm trying to compare two mySQL result sets (for determining when to check a checkbox in an input form). $dataList['listname'] is my needle. (only the checkboxes I want to check) $data['listname'] is my haystack (the list of all checkboxes) Code: [Select] while($dataList = mysql_fetch_array($runListQuery)) { $checked = ''; echo $data['listname'].'<br /><br />'; echo $dataList['listname'].' :: ' . $data['listname'] . '::' . (string)array_search ($dataList['listname'],$data['listname'],false) .'<br>'; echo in_array($dataList['listname'],$data['listname']); echo '<br>----------------------<br>'; var_dump($data['listname']); echo '<br>----------------------<br>'; var_dump ($dataList['listname']); echo '<br>----------------------<br>'; if(in_array($dataList['listname'],$data['listname'],false)) { echo 'found!!!!!!!!!!!'; $checked = 'CHECKED'; } echo '<tr>'; echo '<td>' . $dataList['listname'] . '</td>'; echo '<td><input type="checkbox" '. $checked . ' name="'.$dataList['listname'].'" />'; echo '</tr>'; } I never hit the 'found!!!!!!!!' string. My output is below: Code: [Select] list-charter halwasiya :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(9) "halwasiya" ---------------------- list-charter halwits :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(7) "halwits" ---------------------- list-charter list-charter :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(12) "list-charter" ---------------------- I have a section in one of my scripts that checks for a value in array and the value doesn't exist in the array then it add it. This works fine for single array but not for mulit-dimensional arrays. Has anyone had a similar problem and knows a good solution? Code: [Select] if(in_array($words[$i], $searchWords)){ //Do nothing }else{ $searchWords[] = $words[$i]; } Hi all. I've got this bit of code: $needles=array(`.`,`..`,`css`,`php`,`input_forms`); $t1=scandir($_SERVER['DOCUMENT_ROOT']); foreach($t1 as $t1_value){ if(!is_dir($t1_value)){ unset($t1[array_search($t1_value,$t1)]); }else{ unset($t1[array_search($needles,$t1)]); } } As you can see, I'm trying (in the else statement) to array search $t1 for everything in $needles, then unset the matches from $t1. But its not working and I'm not sure why. I also tried... unset($t1[array_search(in_array($needles,$t1),$t1)]); But that doesn't work either. It only removes the "." directory from the array. Any ideas on what's going wrong? Hello, Currently have an array like looks like the following when using print_r($session->items): Array ( [F1225] => 1 [EDS11147317] => 1 [1156D6RE001] => 1 [I111ADE47946] => 1 [S679AQ339] => 1 [S67914599] => 1 [CH111337631254] => 1 [S6787903647] => 1 [A11144O189] => 1 [F11144520] => 1 [121584Q12] => 1 [I11144661ADE] => 1 [S678829NB] => 1 ) I am trying to check if an item is in the array and if so, display a different result but currently every item says it is in the array when they are not (unless my logic is off...from the items I am looking at some should be included and some should not..but the below code is showing every item as having the same result). Example: foreach ($items as $item) { if (in_array($item->ItemID, $session->items)) { //$session->items is the array output above echo "In Array"; } else { echo "NOT in Array"; } } Currently everything say "In Array" when some should not. Am I using the in_array incorrectly? Thanks Everyone. I can't access an array value in a multidimensional array. The if should be echoing true. $news['companyid'] is equal to 1, but I must not be using in_array correctly. Code: [Select] echo '<pre>'; print_r($own_company); if (in_array($news['companyid'], $own_company)) { echo "true"; } Output Array ( => Array ( [companyid] => 1 [companyname] => Oaysus [companytag] => [companywebsite] => http://oaysus.com [country] => 1 [state] => 0 [city] => [industry] => 4 [stage] => 2 [capitalrequested] => [guestviews] => 0 [iviews] => 3 [eviews] => 3 ) ) Code: [Select] $colorarray = isset($_POST['form']['color']) ? intval($_POST['form']['color']) : 0; $color = array( "1", "2", "3", "4", "5", "6", "7", "8", "8", "9", "10", "11", "12", "13", "14", "15", "16" ); if (in_array($color, $colorarray)) { echo "search found"; exit; }else{ echo "hacker"; exit; } i submit my $_POST['form']['color'] as 1 through 16 and it always says hacker? I am trying to pull the username for everyone who appears within an array. How would i go about this? something like this maybe? SELECT * FROM users WHERE username (in_array($users)) This is PHP 101, but I'm struggling to see the obvious. I have the query below which brings back a list of quizids. I put them into an array using mysql_fetch_array. I then want to check to see if a certain quizid is actually in the quizzes table, so I thought I could simply us in_array. But it's only checking the first quizid. That doesn't make sense to me. Isn't mysql_fetch_array creating an array with all values automatically? If I actually have to use a "while" loop or something, can someone help me with how to write that code most efficiently. Code: [Select] $query = "SELECT quizid FROM quizzes"; $getquizids = mysql_query($query, $connection); if (!$getquizids) { die("Database query failed: " . mysql_error()); } else { $allquizids=mysql_fetch_array($getquizids); if (in_array($_GET['quizid'], $allquizids)) { //continue since quiz exists } else { redirect_to('index.php'); } } hi i wants to skip few categories from xml feed but have problem, here is my code which is working: foreach ($xml->channel->item as $item) { foreach ($item->children() as $child) { if ($child->getName() == 'category') { $categories[] = (string) $child; } } if((in_array("one",$categorie) || in_array("two",$categorie) in_array("three",$categorie)) { continue; } // other code here } and here is what i wants to separate categories in $skipcats but its not working. $skipcats = array("one","two","three"); foreach ($xml->channel->item as $item) { foreach ($item->children() as $child) { if ($child->getName() == 'category') { $categories[] = (string) $child; } } foreach ($skipcats as $skip) { if(in_array($skip,$categorie)) { continue; } } // other code here } please what i am missing. thanks for any guidance . I know this works and that's why I'm puzzled. I'm bringing in data from a textarea and trying to compare it. index.php Code: [Select] <form name"fruit-farm" action="fruit-check.php" method="POST"> <textarea name="fruit-list"> Apples Bananas Oranges Pickles Hamburgers Grapes </textarea> <input type"submit" name="submit" value="Check The Fruit"> </form> fruit-check.php Code: [Select] if (isset($_POST['submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburgers = "Hamburgers"; if (in_array($hamburgers,$fruit-list)){ echo "That is not a fruit"; } } What's puzzling me is that it's not working. It must be something blantently obvious. It has something to do with the '$fruit-list' array and how it is being brought into the form. It posts fine. The reason I think this is the case is because if I simply create an array that is identical to the form coming in it works: Code: [Select] if (isset($_POST['Submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburders = "Hamburgers"; $fruit-list = array('Apples', 'Bananas', 'Oranges', 'Pickles', 'Hamburgers', 'Grapes); if (in_array($hamburders,$fruit-list)){ echo "That is not a fruit"; } } When I 'print_r' both arrays they both look identical. What is wrong and/or different with the way I'm bringing in the textarea that's causing this not to work? I can't seem to understand why in_array is NOT finding the 'needle'. php file: Code: [Select] <?PHP $search = "WS11"; $valid_codes = file("outcode2.txt"); if(in_array($search, $valid_codes)){ echo "yes"; }else{ echo "No"; } echo $search . "<hr>"; echo "<PRE>"; print_r($valid_codes); echo "</pre>"; ?> outcode2.txt: Code: [Select] WS10 WS11 WS12 WS13 WS14 WS15 WS11 is in the outcode2.txt It is not an issue of case. Perhaps my old eyes just keep missing the problem. Any help is appreciated. I have checkboxes (one for each day of the week) that I want to mark checked if the value is in an array. The value of $row['specialDaily'] could be something like 1,2,0 or 1 or 2,3,6 etc. Code: [Select] <?php $weekdays = array($row['specialDaily']);?> <input name="monday" type="checkbox" id="monday" value="1" class="weekday" <?php if (in_array("1", $weekdays)) { echo "checked='checked'"; }?> /><input name="tuesday" type="checkbox" id="tuesday" value="2" class="weekday" <?php if (in_array("2", $weekdays)) { echo "checked='checked'"; }?> /> If the array only contains one value, the proper checkbox is checked. However, if it contains more than 1 value no checkboxes are checked. I just found the in_array() function and assumed it was exactly what I'm looking for. I assumed that since $row['specialDaily']'s value is like an array I could just plug it in. Unfortunately, (unless I'm missing something simple) I'm thinking it doesn't work the way I hoped. Thanks in advance, Twitch Can someone please help me figure this out: I have a normal text file with 3 lines in it, every line contain one date i.e 12112011 (12.11-2011). I put the content of that file in an array, and want to compair the dates in the text file against the date generated from the loop. Problem is, i can only get a match for the last date listed in the text file no matter what.. If i have 4 dates in the file, it only style the last date. Thanks! --- CODE SNIPPET --- $day_num = 1; $days_in_month = 30; $month = 11; $year = 2011; //Access the file containing already taken dates and load them in an array: $date_array = file("test.txt"); //count up the days, untill we've done all of them in the month and check them against the dates in the text file: while ($day_num <= $days_in_month) { // Set the variable $date to be checked against the content of the array: $date = $day_num . $month . $year; if (in_array($date, $date_array)) { echo "<td class='color_green'>$day_num</td>"; //print_r($date_array); } else { echo "<td>$day_num</td><br />"; } $day_num++; } Hey guys, I have this super simple code which will not work and I'm very confused as to why. Here is the code Code: [Select] // read the file into an array called $users $users= file('users.txt'); if (in_array("joe", $users)) { echo "Got Joe"; } When I print_r($users) everyone looks fine like this but it never returns true, Why? Code: [Select] Array ( [0] => joe [1] => bob [2] => mike [3] => ted [4] => fred ) I have something like below Code: [Select] $j = explode("-", $ROW['options']); if(array_search("src_str", $j) == false) echo '_no'; // This doesn't work Also i tried Code: [Select] $j = explode("-", $ROW['options']); if(!in_array("src_str", $j)) echo '_no'; // This also doesn't work Is this bug or am i doing something wrong? Okay, this is my loop to show the birthday for Years. Code: [Select] <?php for ($i=2006; $i>=1900; $i=$i-1) { echo "<option value='$i'>$i</option>"; } ?> It will show 2012 to 1900 as a dropdown menu.. in my select form. Now I want to check the user input against this loop. The form is: Code: [Select] <select name="BirthDay[]"> <?php for ($i=2012; $i>=1900; $i=$i-1) { echo "<option value='$i'>$i</option>"; } ?> </select> Now, I want to use in_array function to check if it equals any of the values I looped, if it doesn't echo out "Hacker!" Here is my in_array Code: [Select] $bday = array( NEED TO PUT THIS LOOP HERE SOMEHOW?!?! ); if (!in_array($_POST['BirthDay'], $bday)) { message("Please select a Correct Birthday, or u trying to hack"); } Thanks if u can help! Hello, I've been curious why the following won't work. Could anyone show me what I am doing wrong. The code is to find the TLD in a URL. Code: [Select] $a[] = parse_url( 'www.example.com' ); $tld_arr = array ( 'com', 'net', etc...); for ($i=0; $i<count($this->a); $i++) { if (in_array ( $this->a[$i]['host'], $tld_arr) ) < This is what I'm curious about { //do something } // do something else } Thank you I have the following simple code to test against collision on a primary key I am creating: Code: [Select] $machine_ids = array(); for($i = 0; $i < 100000; $i++) { //Generate machine id returns a 15 character alphanumeric string $mid = Functions::generate_machine_id(); if(in_array($mid, $machine_ids)) { die("Collision!"); } else { $machine_ids[] = $mid; } } die("Success!"); Any idea why this is taking minutes to run? Anyway to speed it up? Hi there, I am currently using the in_array() function to check the user's country with the country in the ShippingProfile table. I have a function that returns the shipment profile info from the table. $myshipProfile = filter_shippingprofile(array(......)); And in the $myshipProfile I get the profile info with the countryID. Now I can check the user's country in the array: if(in_array($_SESSION['userCountryId'], $myshipProfile)) { print "yes country is found"; } I works fine so far. But there is a slight problem to it. I did a quick: print_r($myshipProfile); And the output was: Array ( [shippingprofileID] => 14 [supplierId] => 66 [shippingprofilename] => ProfName1 [shippingto] => CustomCountries [ShipRegionCountries] => 66 [shippingcost] => 10 Notice the supplierId and the ShipRegionCountries has the same value. And now when I try to perform in_array() it will return true every time even if the ShipRegionCountries is not 66 because as supplierID is 66. You see the conflict. Therefore, can we write like this: if(in_array($_SESSION['userCountryId'], $myshipProfile['ShipRegionCountries'])) { print "yes country is found"; } I get no ouput for this. Whats the correct way to check only the 'ShipRegionCountries' in the array? Thank you Hi There, I have the following code: two arrays: $user_body_group & $DB_Seconday_muscles I want to look up the "$DB_Seconday_muscles" Array and search for the elements in the "$user_body_group" array The result would be displaying a checkbox list with only the items in the "$user_body_group" array checked: foreach($DB_Seconday_muscles as $value) { echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$value\""; if (in_array($value,$user_body_group)) { echo "CHECKED"; } echo "> $value "; } BUT... When i run thi script i only get the first element in the searched array ticked. Can anyone help! Many Thanks! Leatfield |