PHP - Multidimension Array + In_array
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 Similar Tutorials<?php $menu = array( '22' => array ( 'title'=>'Businesses' ,'busstype'=>array( 5=>'Printers' ,2=>'Bookshops' ,6=>'Publishers' ,8=>'Uniform Distributors' ) ) ,21 => array ( 'title'=>'Academic' ,'busstype'=>array ( 5=>'Academy' ,6=>'Primary' ,7=>'Polytechnic' ,11=>'University' ) ) ,'Events' => array ( 'title'=>'Events' ,'busstype'=>array ( 3=>'Music' ,4=>'Road shows' ,6=>'Sports' ,6=>'Yoga' ) ) ) ; I have a dynamically created menu as above. How would i sort on the 'busstype' homogenously and print it while maintaing the key. Results should be like <a href="search.php?listing=21&busstype=5">Academy</a> <a href="search.php?listing=22&busstype=2">Bookshops</a> ....... <a href="search.php?listing=Events&busstype=6">Yoga</a> ?> I'm having an issue with building a multidimensional array via a for (....) the array should look like the example any help would be much, much appreciated thanks siouxx array ["username"] = "steve" array ["item"] = "12345" array "model" = "some model" "quantity" = "36.2" "details" array 0 = "detail 1" 1 = "detail 2" 2 = "detail 3" ["username"] = "steve" array ["item"] = "000257" array "model" = "some other model" "quantity" = "77.8" "details" array 0 = "detail 1" 1 = "detail 2" ["username"] = "bob" array ["item"] = "45678" array "model" = "other model" "quantity" = "99.5" "details" array 0 = "detail 1" 1 = "detail 2" 2 = "detail 3" 3 = "detail 3" the array return by sql looks like this Array ( => Array ( [id_prod] => 6 [id_tipo] => CPT [marca] => HP [modello] => Pavilion p6-200 [prezzo] => 599.00 [desc] => Computer da Tavolo [chiave] => 32 [utente] => steve [qta] => 1 [indata] => 2011-10-10 16:32:20 [dettaglio] => HD 320GB - RAM 4GB - Scheda audio 5.1 ) [1] => Array ( [id_prod] => 6 [id_tipo] => CPT [marca] => HP [modello] => Pavilion p6-200 [prezzo] => 599.00 [desc] => Computer da Tavolo [chiave] => 31 [utente] => steve [qta] => 1 [indata] => 2011-10-10 16:32:20 [dettaglio] => Intel Core i3-550 (3.20GHz, 4MB L3) I have whiled my way through any number of forums and help pages so far. However, since I am not quite comfortable yet with PHP, I am not sure which option I have been given would be the right one. What I am trying to do: Create the source for a set of drop-down menus. The dropdown menu has columns, and then a H2 and P that go in each column, in varied numbers, so you have column1, h2 p; h2 p; h2 p column2, h2 p; h2 p etc....make sense? I am using an MySQL table as source so I can have a little CMS dashboard where I can change these and other values on the fly. So I have a MySQL table where I have a field that identifies the column (page name), and then values to fill the h2 and p tags. I.E.: page_name, content_title, content_text The idea, therefore, is to use PHP to figure out what the column names (that are repeated) in the mysql table and then create the dropdown based on the value in the subsidiary fields. Output is as a string in a variable value that is output via return (); My best estimation of how to do this is to use php to create a multidimensional array from the query result, where the first column is the page_name, followed by an array, the array being the remainder of the information. Based on information scrounged from other sources, I have come up with this so far: $query = "SELECT * FROM tbl_mainmenu WHERE mainmenu_type = '$mmtype'"; // what the query is $result = mysql_query ($query, $dbc); // connect to database and run query, setting the the result to dummy array variable '$result' // establish the multidimensional array $mmcontent_array = array (); //create the array where the values are content page and associated values in an array per each content page while ($result = mysql_fetch_assoc ()) { $page_name = $result['content_page']; $mmcontent_array['content_page'] = $result; } foreach ($mmContent_array[] as $key=>$value) { foreach () { $tmpelements[] = '<div class="col_2"><h2>' . $element[content_title] . "</h2>\n<p>" . $element[content_text] . "</p>\n</div>"; $elements = implode("\n",$tmpelements) . "\n" } $tmp[] = '<li><a href="#" class="drop">Products</a><!-- Begin Products Item -->\n<div class="dropdown_2columns"><!-- Begin 2 columns container -->' . $elements . 'whatever tags will close this') } Am I even in the ballpark? I am not sure which way to nest the foreach ()'s. The example I was working from used echo ()'s, and I am not sure if using $variable = implode () makes the task different. As a start, I am just wondering if I am creating the multidimensional array correctly? 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++; } 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? 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, 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. 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 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 ) ) 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 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'); } } 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)) 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. 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" ---------------------- 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 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]; } 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 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? 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 ) 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 |