PHP - In_array Not Working--trouble With Needle Array
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? Similar TutorialsHello, 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. 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 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]; } Hello: 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" ---------------------- 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++; } 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 Im getting a warning with in_array.I have used if() inside for loop to check if value has already been printed or not. If not it will print and save the value in $done[].Can someone please point me out my mistake that i am getting a warning. Code: [Select] <?php for($i=0;$i<=$count;$i++) { $count_final = explode("_", $result[$i]); if (in_array($count_final[0],$done)) { //Line 480 echo "<tr>"; echo"<td>$count_final[0]</td>"; $templ = $count_final[0]."_left"; $tempr = $count_final[0]."_right"; $cleft = $count_middle[$templ]; $cright = $count_middle[$tempr]; echo"<td>$cright</td>"; echo"<td>$cleft</td>"; echo "</tr>"; $done[] = $count_final[0]; } } ?> Code: [Select] Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Users\\Desktop\yyy.php on line 480 Hello I'm using tcpdf to convert some stuff to pdf. On my local computer running wamp my script works perfect and the pdf is generated without problems When i put my files in a webserver i got this error: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/vieira/public_html/topdf/dbcon.class.php on line 68 Line 68 is: if(in_array($key,$arr_switches)){ //check switches My complete code is: // Get fields of enum on/off switches type $result2 = mysql_query("SHOW FIELDS FROM $db_name.$table"); $counter=0; while($row = @mysql_fetch_array($result2)){ //echo $row['Field'] . ' ' . $row['Type']."<br/>"; if($row['Type'] === "enum('on','off')"){ $arr_switches[$counter] = $row['Field']; $counter++; } } //print"<pre>"; print_r($arr_switches); print"</pre>";exit; $counter = 0; while($row = @mysql_fetch_array($result)){ foreach( $row as $key => $val ){ if(!is_numeric($key)) { $row_rs_certidao[$key] = $val; if(in_array($key,$arr_switches)){ //check switches $record_key[$counter] = htmlentities('<?php if (!(strcmp($row_rs_certidao['."'".$key."'".'],"on"))) {echo "x";} ?>'); if($val==='on') $record_val[$counter] = "x"; //turn on switches else $record_val[$counter] = ''; //turn off }else{ $record_key[$counter] = htmlentities('<?php echo $row_rs_certidao['."'".$key."'".']; ?>'); $record_val[$counter] = htmlentities($val); } $counter++; } } } Anyone can help? I have a string that is unformatted and has a lot of commas in it. How would I add a \n linebreak after every 4th comma? Hello, I need to find the target html address within some raw html. I know it lies between Needle 1 and Needle 2 below. Needle 1: </span></a> <a href=" Needle 2: " rel="nofollow"><span class=pn><span class=np>Next »</span></span></a></div> Nornally I would just use the explode() function but my headache is that there are many instances of Needle 1 in the raw html code and I want my output array always have the same number of elements. However I do know that my target html address is directly after the last instance of Needle 1. How should I go about splitting the html code into an array that always has 3 elements? I'd like the first element to include code up to and including the last instance of Needle 2, the 2nd element to have the target html address, and the 3rd element to have Needle 2 and any following code. Thanks for any pointers (my main struggle is identifying the last instance of needle 1), Thanks, Stu I've never used array's because I've always found a way to do something without them. I have finally taught myself how array's work, but I cannot seem to add a key and value to an array without my code creating another array inside that one. I'm not sure what I'm doing wrong, but this is what I'm getting. From what I understand, print_r is suppose to print out an array. This is what my print_r displays: Array ( [knife] => 2000 ) Array ( [baseballbat] => 2000 [knife] => 2000 ) Array ( [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 ) Array ( [deserteagle] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 ) Array ( [ingram] => 2000 [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 ) Array ( [deserteagle] => 2000 [ump40] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ingram] => 2000 ) Array ( [colt] => 2000 [ingram] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ingram] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [colt] => 2000 [ump40] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ingram] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ump40] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [colt] => 2000 [ingram] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ingram] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [ump40] => 2000 [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [ingram] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [ump40] => 2000 [ingram] => 2000 [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [grenades] => 958 ) Array ( [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 [ingram] => 2000 [ump40] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [teargas] => 1800 [grenades] => 958 ) Array ( [pepperspray] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [ingram] => 2000 [ump40] => 2000 [baseballbat] => 2000 [colt] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [grenades] => 958 ) Array ( [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [ump40] => 2000 [baseballbat] => 2000 [ingram] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 ) Array ( [ingram] => 2000 [pepperspray] => 2000 [flashbangs] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [ump40] => 2000 [ingram] => 2000 [knife] => 2000 [deserteagle] => 2000 [colt] => 2000 [baseballbat] => 2000 [pepperspray] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [flashbangs] => 2000 [colt] => 2000 [pepperspray] => 2000 [deserteagle] => 2000 [ingram] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [deserteagle] => 2000 [baseballbat] => 2000 [knife] => 2000 [pepperspray] => 2000 [ingram] => 2000 [colt] => 2000 [ump40] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Notice how the first array has one key/value, the second has the same value as the first plus an additional value. The third array has the same values as the second array plus an additional value. I'm confused as to whether my array is correct, and that print_t is just showing me how the array has changed everytime the while loop went through, or there is an error in my code and my array is not correct. I want to get this cleared before I continue. Here is my code so far: Code: [Select] <? $user = $_GET['user']; $attacks = $_GET['attacks']; $quser = mysql_query("SELECT * FROM user WHERE username = '$user'"); $nuser = mysql_num_rows($quser); if ($nuser == 1) { $ruser = mysql_fetch_array($quser); $totalpower = $ruser[power] + $ruser[defense]; $sabpower = round ($totalpower * $sabdmg); $ramount = mysql_fetch_array(mysql_query("SELECT * FROM user_items WHERE username = '$user'")); $wa = array(); $qweps = mysql_query("SELECT * FROM weaponstore ORDER BY type ASC, price ASC"); while ($rweps = mysql_fetch_array($qweps)) { $wepname = $rweps[colname]; $wepamount = $ramount[$wepname]; if ($wepamount >= 1) { $wa[$wepname] = $wepamount; } arsort($wa); print_r($wa); } } ?> Basically the code should add the $wepname as the key and $wepamount as the value to an array $wa only if $wepamount is >= 1, but the way my print_r is showing it I'm just confused as to why I'm getting a bunch of arrays and not just one. hi, i am pulling data from a database, a picture and some text, i am able to have the records display vertically but i want them to display across the screen rather than downwards, but because of the page size, i only want 2 results per line. does any one know how i can do this? thanks in advance. Des This is the code i am currently using $result = mysql_query("select * from staff'"); echo "<table border='0' cellpadding='10' cellspacing='0'>"; echo "<tr>"; while($row = mysql_fetch_array($result)) { echo "<td><img src=../upload/".$row['Pic']." border='0' width='140' height='105' /></td>"; echo "<td>".$row['Name']."</br><b>".$row['Role']."</b></td>"; } echo "</tr>"; echo "</table>"; Hey, I am trying to make and then read an array of items. Can someone help please? I have a database field that holds an unlimited number of codes, and I need to get those codes.... at the moment I have a section of php $products= array($products); $array = array_values($products); print_r($array); which generates Array ( => FRE0001,FRE0009,FRE0009,FRE0024,FRE0024 ) as the output... What I need is to know how many of each code there are and the different codes.... i.e. 1 x FRE0001, 2 x FRE0009, 2 x FRE0024 Whats the best way to output the array and then count? Hi, learning as I go here, and I appreciate the help in advance .. I have some working php code that retrieves sql results using php. I assign the resutls to an array by doing this $bobreport = array(); while ($row=db2_fetch_array($querystmt)) { array_push($bobreport, $row); } i can then write the rows of returned data after some column headers using this... foreach ($bobreport as $value) { echo "<tr><td>$value[0]</td><td>$value[1] ..... } The particular report I'm working on now has a redundant date and time in every row that I omit in the foreach loop because I just want to show it once above the table somewhere. This doesn't seem to work in accomplishing that... echo "This data was updated ".$bobreport[0][9]." at ".$bobreport[0][10] ; All I seem to get in the report is "This data was updated at". Please help. Hi guys, Need some help with arrays! The following code puts ALL dates into an array, or should I say should. Because this: do { $start = $row_getCalendarDates['page_date']; $end = $row_getCalendarDates['page_date02']; $init_date = strtotime($start); $dst_date = strtotime($end); $offset = $dst_date-$init_date; $dates = floor($offset/60/60/24) + 1; $booked=0; while ($booked < $dates) { $newDateFinal[] = date("Y-m-d", mktime(12,0,0,date("m", strtotime($start)), (date("d", strtotime($start)) + $booked), date("Y", strtotime($start)))); $booked++; } print_r($newDateFinal); } while ($row_getCalendarDates = mysql_fetch_assoc($getCalendarDates)); Infact prints something like so when i have multiple results: Array ( [0] => 2010-12-01 [1] => 2010-12-02 [2] => 2010-12-03 ) Array ( [0] => 2010-12-01 [1] => 2010-12-02 [2] => 2010-12-03 [3] => 2010-11-01 [4] => 2010-11-02 [5] => 2010-11-03 [6] => 2010-11-04 [7] => 2010-11-05 [8] => 2010-11-06 [9] => 2010-11-07 [10] => 2010-11-08 ) These are 2 results coming from the database, so what it is doing is putting both into arrays, but I 'NEED' them iin a single array, how on earth do I go about doing this? Note however, how the first array has the first set of results but the second has them all ... this is affecting my code and so, need to somehow reference and get rid of the first array. Hope someone can help! Regards, Joe. Hello everyone, I have a question with the use of the functon in_array... I have a list of categories in my table and I want to know if a specific category exists so I am doing this: $categoriasexistentes= mysql_query("SELECT * FROM LugaresTuristicos WHERE DestinoPeg='$LinkDestino' GROUP BY Clasificacion ") or die(mysql_error()); $array = mysql_fetch_array($categoriasexistentes); print_r($array); WHAT IS DISPLAYED BY print_r (there should be much more categories) IS THIS: Array ( => Bar [Clasificacion] => Bar ) If I ad this all categories are displayed: while($categoria = mysql_fetch_array($categoriasexistentes)) { $todas=$categoria['Clasificacion']; ?> <?php echo"{$todas} - "; ?> <?php }; ?> Why wont my radio array show the value that was selected. If a user select a radio then the value should be printed. But it should print the question id then the choice that was chosen for that question id. Code: [Select] <?php $gender = $_POST["choice"]; $que_ID = $_POST["Que_ID"]; foreach ($gender as $key => $value) { echo $key." - ".$value."";} ?> Code: [Select] $intNum = 1; $intnumber = 1; while( $info = mysql_fetch_array( $sqll )){ echo "<input type='hidden' name=\"Que_ID\" value=\"{$info['Que_ID']}\" /> "; echo " $intNum, {$info['Que_Question']} <br />\n"; $intNum++; for ($i =1; $i < 5; $i++) { echo "<input type=\"Radio\" name=\"choice[{$info['Que_ID']}][]\" />{$info['Que_Choice'.$i]}<br />\n"; $intnumber++; hi. Basically, I am trying to set up a random bible verse script. I found an rss/xml feed he http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV Using the following code, I am attempting to parse the feed in to an array: Code: [Select] <?php function fetchXML($url) { //initialize library $ch = curl_init(); //used to make us look like a browser $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; //set the url curl_setopt ($ch, CURLOPT_URL, $url); //set that I want to wait for a response curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); //make us look like a web browser curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); //get data $data = curl_exec($ch); //clean up curl_close($ch); //return xml data return $data; } define("USE_CURL", true); $url = "http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV"; //get xml doc with info $data = fetchXML($url); //create a SimpleXML object to parse the xml $char_xml = new SimpleXmlElement($data); echo print_r($char_xml); ?> Which gives me the following output: Code: [Select] SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => My Bible Scripture - Bible Verse of the Day (KJV) [link] => http://www.mybiblescripture.com/ [description] => Verse of the Day - King James Version [language] => en-us [pubDate] => SimpleXMLElement Object ( ) [generator] => CPG-Nuke Dragonfly [copyright] => My Bible Scripture [category] => Religion [image] => SimpleXMLElement Object ( [width] => 111 [height] => 40 [url] => http://www.mybiblescripture.com/images/logo.gif [title] => My Bible Scripture [link] => http://www.mybiblescripture.com/ ) [item] => Array ( [0] => SimpleXMLElement Object ( [title] => Corinthians I 10:17 [link] => http://www.mybiblescripture.com/Bible/l_op=view-verse/lid=29613/trans=KJV.html [description] => For we being many are one bread, and one body: for we are all partakers of that one bread. (KJV) [pubDate] => SimpleXMLElement Object ( ) ) [1] => SimpleXMLElement Object ( [title] => Bible Verses [link] => http://www.mybiblescripture.com [description] => Find more Bible Scripture [pubDate] => Fri, 11 Mar 2011 00:45:06 GMT ) ) ) ) 1 What I am trying to do is pull ONLY [title] => Corinthians I 10:17 and its accompanying verse [description] => For we being many are one bread, and one body: for we are all partakers of that one bread. (KJV). I am very bad at trying to read multi-dim arrays and can't for the life of me figure out how to print the aforementioned keys. Could anyone help me here? thanks The code below is showing "undefined" and "OBJECT" errors
What am I doing wrong to build this multi-dimensional array?
And why can't I paste it to this page???????????????????????
|