PHP - Finding The Highest And Lowest Number In An Array?
Hi folks, i have a complex problem (for me anyway) but i am sure with your help this can be sorted.
Problem number 1: I have a simple while loop, but its only showing 4 results when i know there are 5 I am missing something very simple i know: $sql = ("SELECT * FROM postcode WHERE unid='1'")or die ('<p>There seems to be a problem, please try again soon.</p>'); $result = mysql_query($sql,$con); $row = @mysql_fetch_array($result); $i=0; while ($row = @mysql_fetch_assoc($result)){ echo "newpoints[". $i++ ."] = new Array({$row['lat']}, {$row['long']}, icon0, '{$row['postcodename']}', '{$row['postcodename']}'); \n"; } What am i missing here? Problem number 2: I have a bunch of latitude and longitude codes stored in the database. On each page there will be a varied amount of these codes used, but with a max of 20. I need to find the highest number and the lowest number of each lat and long then come up with a code that is in the middle, the idea of this is to center a map. So for example there will be an array like this: 50.852293 -1.76088 51.252938 -0.76128 51.259583 -0.727168 51.274 -0.837 51.123106 -0.970657 First i need to find the highest number of the first code (51.274) then the lowest number (50.852293) and then the highest number of the second code (-1.76088) then the lowest number (-0.727168) Then work out an inbetween code using the highest and lowest codes for each. Does anyone have a clue how to go about this? I am not the worlds best array technition or mathematician Similar Tutorials(1) I need to remove the lowest and higest values from an array. I need to actually remove the two lowest and two highest. I suppose the best thing would be to sort the array and then use a function that just drops the first and last values (twice). Is this the best way? Which function would drop the lowest / highest values? (2) I then need to get the remaining first and last (highest and lowest) values. $array[0] would get first, and revesing the order and $array[0] again would get the last. Is this the best way? I have a row in a MySQL table called id, which auto-increments. I was wondering if anyone knows a PHP solution to find out what the highest number in the row is? I am thinking perhaps execute a MySQL query that will list the highest number, and then echo the result, like $row[id] in PHP? Hello, I need to set a CSS class for the highest number in a MySQL column, and the second highest. So for instance I have a column named scores. I would want the <td> with highest scores to be <td.Winner> and the second highest to be <td. RunnerUp>. I know how to set the css part up, so just need help with creating a function. I did something similar to set the background of table cells based on the text data and that looked like this. function cssfromdate($date) { $class = array('December_January' => 'January', 'March_April' => 'March'); return $class[$date]; } while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['last_name'] . "</td>\n"; echo "<td>" . ucwords($row['first_name']) . "</td>\n"; echo "<td class=\"".cssfromdate($row['Class_Date'])."\">".$row['Class_Date']."</td>\n"; echo "</tr>"; } I am hoping I can accomplish what I need to doing something similar. I have build a small form that take the zip code that user enter and if is on my list will redirect to a sub domain craigslist.
I was checking all the zip codes and is pretty hard to know to what bigger city to attribute the given zip code and for that I was thinking to a simple idea to use, I am not sure how much exact will be but I think will do the trick for the moment.
The zip code range start from 00501 to 99950.
I create a array with the biggest city shown in craigslist and the zip code that correspond.
Now the tricky part, what I will do with the rest of the zip codes and my idea is to measure the distance from one to another zip code and which zip code from the array is closer from given one to take it.
<?php $zip = $_GET['zip']; $z = array ( 36830 => array ( 'auburn.craigslist.org' ), 35242 => array ( 'bham.craigslist.org' ), 36301 => array ( 'dothan.craigslist.org' ), 35630 => array ( 'shoals.craigslist.org' ), 35901 => array ( 'gadsden.craigslist.org' ) ?> Exemple: $zip= 36870 // the code received from the user 36870 - $array= ( 'auburn.craigslist.org' ), (36830 = 40) (36301 = 569) (36911 = -41) // the minus will be excluded (....etc)After checking the zip code with the ones from array, the smaller score will attribute the sub-domain correspond from the array. I hope will don t be the case when will get the same result from two zip codes:) Now I am using this rule: $zip0 = (isset($z[$zip][0]) && $z[$zip][0] != '') ? $z[$zip][0] : 'craigslist.org/about/sites#US';and I was thinking to redirect the user for a page to enter the state and the city. But I wish to make it more precises and not to involved the user so much. But if there are any other suggestions I will be glad to hear them. Hey, I have an array which stores a bunch of numbers lets say: Code: [Select] [0] = 1[1] = 6 Now a second array has: Code: [Select] [0] = 5 [1] = 6 What im trying to do - is find the first highest number from the second array that is not found in the first array.Both arrays are sorted already by the way using sort(). What is the best way to do this? Hi Guys
I'm a bit stuck on a query and hoping someone can help. I'm sure this has a really simple solution.
Below is a simplified representation of a table and I'm trying to select all of the data but where there are instances of the same postID I want to only select the row with the highest version number.
So for instance in a select all on the table below I would expect to get IDs of 2,3 and 4.
id | postID | version I have mysql select query shown below retrieving data from a database:
$sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } }On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3)How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback: Warning: max(): When only one parameter is given, it must be an array Thanks. I am designing a mmorpg guild site, and decided to make a coordinate database. A user will enter their own coords, x and y, and the 10 closest coords to them will be displayed. I need help doing this. My table is: id coordx coordy alliance region I am using $_GET to retrieve the user's input. so $_GET['mex'] $_GET['mey'] Thanks in advance for your help. I currently have an array that I've built that dumps like this:
0 => array:11 [▼ "category_code" => "123" "category_name" => "Testing" "category_description" => "This is a test category" 19738 => array:5 [▼ "identifier" => "720368842943" "description" => Test Description One "count" => 4 "details" => array:2 [▼ 0 => array:3 [▼ "detail_code" => "2751" "detail_code2" => "43" "detail_specifier" => "Detail One" ] 1 => array:3 [▼ "detail_code" => "2681" "detail_code2" => "9" "detail_specifier" => "Detail Two" ] ] "prices" => array:1 [▼ "01" => "1129.00" ] ] 19739 => array:5 [▼ "identifier" => "720368844121" "description" => "Test Description Two" "count" => 4 "details" => array:2 [▼ 0 => array:3 [▼ "detail_code" => "2751" "detail_code2" => "43" "detail_specifier" => "Detail One" ] 1 => array:3 [▼ "detail_code" => "2681" "detail_code2" => "9" "detail_specifier" => "Detail Two" ] ] "prices" => array:1 [▼ "01" => "1490.00" ] ] I'm using laravel excel in order to export that as an excel file, but it's not quite working the way I intend When it exports to excel I only get the top level info: 123 | Testing | This is a test category But I want to get that info as a header and then each subsequent product for that category as a row, so with the example above it would look like:
123 | Testing | This is a test category Here's the excel code with the array I'm using, which is dumped above: $allCategoryResult= array(); foreach($prices->categories as $category){ $categoryItem = array(); $categoryItem["category_code"] = $category->category_code; $categoryItem["category_name"] = $category->category_name; $categoryItem["category_desc"] = $category->category_desc; foreach($category->skus as $sku){ $skuItem = array(); $skuItem["identifier"] = $sku->sku_info->identifier; $skuItem["description"] = $sku->sku_info->item->description; $skuItem["count"] = $sku->sku_info->item->item_type->count; $skuItem["details"] = array(); foreach ($sku->sku_info->details as $details) { $detailsItem = array(); $detailsItem["detail_code"] = $details->detail_code; $detailsItem["detail_code2"] = $details->detail_code2; $detailsItem["detail_specifier"] = $details->detail_specifier; $skuItem["details"][] = $detailsItem; } $skuItem["prices"] = get_object_vars($sku->prices); $itemCode = $sku->sku_info->item->item_code; $categoryItem[$itemCode] = $skuItem; } $allCategoryResult[] = $categoryItem; } $name = 'Test Export'; $build = Excel::create($name, function ($excel) use ($allCategoryResult) { $excel->setTitle('Test Export'); $excel->sheet('Test Export', function ($sheet) use ($allCategoryResult) { $sheet->fromArray($allCategoryResult);
I am trying to locate an array in an array of arrays:
$card_pos = array_search($target, $_SESSION['leitner']['boxes'][($box - 1)]); if ($card_pos !== false) { // Do my stuff here }echo '<pre> oh no -- didn\'t find '; print_r($target); echo "in ";print_r ($_SESSION['leitner']['boxes'][($box + 1)]);echo '<br>leitner';print_r ($_SESSION['leitner']); echo '</pre>';but I don't find the target in the array though it is clearly there as you can see in the output: oh no -- didn't find Array ( [pos] => v5g [tense] => agerimasu ) in Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) leitnerArray ( [boxes] => Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( [0] => Array ( [pos] => v5g [tense] => agaru ) [1] => Array ( [pos] => v5g [tense] => agarimasu ) [2] => Array ( [pos] => v5g [tense] => agarimasen ) [3] => Array ( [pos] => v5g [tense] => agaranai ) [4] => Array ( [pos] => v5g [tense] => ageru ) [5] => Array ( [pos] => v5g [tense] => ageranai ) [6] => Array ( [pos] => v5g [tense] => agerimasen ) [7] => Array ( [pos] => v5g [tense] => agerimasend ) [8] => Array ( [pos] => v5g [tense] => agereba ) [9] => Array ( [pos] => v5g [tense] => agareba ) [10] => Array ( [pos] => v5g [tense] => agatte ) [11] => Array ( [pos] => v5g [tense] => agette ) ) [4] => Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) [5] => Array ( ) ) [denom_total] => Array ( [1] => 0 [2] => 0 [3] => 36 [4] => 40 [5] => 40 ) [denom_max] => 40 ) Hi, I have a Comma Delimited File called "houses.txt" with the contents of: HA11QS, 200, house1.jpg, 4 HA22BR, 280, house2.jpg, 10 HA33AB, 390, house3.jpg, 3 HA44CD, 320, house4.jpg, 8 I have a web form "form2.html": Code: [Select] <html> <head> <title>Untitled Document</title> </head> <body> <form action="any.php" method="post"> Please Enter Anything <input type="text" name="any"> <input type="submit" value="Submit"> </form> </body> </html> and PHP code of "any.php": <?php if (isset($_POST['any'])) { $filename = "houses.txt"; $fileOpen = fopen($filename, "r"); $max = $_POST['any']; $rowsArr = file ($filename); foreach ($rowsArr as $row) { $lineDetails = $row; $item_array = explode (",", $row); if (in_array($max,$item_array)) { echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); echo("<br>"); } } fclose($fileOpen); } ?> What i need is for the user to input anything they wish for example: 4, which would search the array and find that the first house has had 4 visits or HA44CD, to find the last house on the list etc ... however unfortunatley its not working for, if anyone can help me i would grateful Thank You Hi i have two arrays, activity and feed.. contains 100 arrays, $activity = array(0,1,2....,100); contains 5 arrays $feed = array(100,50,25,12,75); I have 7 ranges, (0 -15),(15-30),(30-45),...(90-105) now i need to compare feed array with activity array to find in which range it was... Example, $feed[0] = 100, then it is in (90-105) range $feed[3] = 12, then it is in (0-15) range.. ... how to find this range?? please help me.. Hi PHPfreaks, I've got a problem Ive been faced with a few days now, and for the life of me Im unable to solve it. What I have is a multidimensional array, that I need to parse and retreive every possible outcome possible Below is a basic example of how the array would look Code: [Select] Colour Black White Blue Finish Matt Gloss What I need to be able to do is, as I said, Show all the possible combinations there is. below is what i'd have achieved after one of you helpful souls points me in the right direction. Code: [Select] --> Color: Black --> Color: White --> Color: Blue --> Finish: Matt --> Finish: Matt --> Color: Black, Finish: Matt --> Color: White, Finish: Matt --> Color: Blue, Finish: Matt --> Color: Black, Finish: Gloss --> Color: White, Finish: Gloss --> Color: Blue, Finish: Gloss As you can see, Only ONE (or no) value is picked from each section. Just to confirm: the number of option groups WILL change...... So I could have colour, finish, size, weight and many more. the number of values in the groups WILL change....... so under colour, I could also have pink, green, indigo, gray. Below is how this array is structured Code: [Select] Array ( [0] => Array ( [parent] => Colour [values] => Array ( [0] => Array ( [name] => Black ) [1] => Array ( [name] => White ) ) ) [1] => Array ( [parent] => Finish [values] => Array ( [0] => Array ( [name] => Matt ) [1] => Array ( [name] => Gloss ) ) ) ) Would anyone have any suggestion on how this can be done? I've pulled out every single strand of hair I've got now, so thought i'd admit defeat and let someone else have a go Many thanks inadvance. Good day, Is there a simple way to find and list duplicates i an array and insert these to a new one? I have: $array[0] = "asd" $array[1] = "ter" $array[2] = "asd" $array[3] = "asd" $array[4] = "xfh" From that, I need this: $newarray[0] = "0" $newarray[1] = "2" $newarray[2] = "3" I have poked around "array_not_unique" and "in_array" a bit, but can't wrap my head around it enough for it to work... Any and all help are much appreciated. Thanks in advance! I am having a go at writing my own simple gallery. As the I want to display an image from a folder in a directory of all the images... for example, I have 10 folders each containing images. I want my code to take image 01.jpg out of each folder and display it on my page, so that when I add folders to my main images folder, they will automatically be added to the page... but I only want the last 10 folders to display... so once I have 20 folders, it only shows 10. I also want the images to be 5 across and then make a new row. My folders will be numbered in order... 001, 002, 003, etc... and I need them sorted in reverse order, so the folder added last is displayed first. So... this is where I am at with the code : Code: [Select] $dir = "gallery/"; if (is_dir($dir)) { [color=red] foreach(array_keys($file) as $n) {[/color] if ($n+1 == 5) { echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td></tr><tr>"; } elseif ($n+1 > 10) { echo ""; } else { echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td>"; } closedir($dh); } } For some reason, I am getting the following error on the page : Warning: First argument to array_keys() should be an array in /path/to/gallery.php on line 8 Warning: Invalid argument supplied for foreach() in /path/to/gallery.php on line 8 line 8 is the one in red above. So... I have a couple of questions... 1. What have I done wrong so far? I cannot see it 2. How would I then go about putting the galleries in reverse order? I know it must be rsort, but not to sure if I need to do this first or after... ? Any help greatly appreciated, and I would also be greatful f an idiot's explanation, as I am really trying to understand all this! I'm getting there... slowly! I have a list of IP addresses in an very very very large array in random order. I am trying to find a way to determine if there are at least 8 IP addresses that are consecutive. I was thinking some sort of for loop after a sort but that seems very time consuming and I'm sure PHP has a better way... Some searching online revealed this: http://bytes.com/topic/php/answers/12143-flagging-consecutive-numbers-data-set Is this the way to go? Any tips? Thanks! Carmen Does anyone know a way of to find the 'coordinates' of a deeply nested key in a multidimensional array whilst using array_walk_recursive? I am using array_walk_recursive() to stripslashes() of all values of an array. However I need to be able to get the full coordinates of a key if the said key is of a certain value. The function key() only seems to get the highest level key. As a test I have the following array which has three occurrences of a key named 'date'. Upon each occurrence I want to be able to know exactly where in the array it is so i can call is directly later. <?php $pages = array(array("variation"=>"xccsdc", "items"=>array(array("nm"=>"ss", "type"=>"ta", "kkl"=>"Prodion." ), array("nm"=>"nm", "type"=>"nwsfd", "kkl"=>"nws fd", "nws"=>array(array("date"=>"1328982639", "kkl"=>"f nfgfgnfgn", "title"=>"nfgn fgn fhn " ), array("date"=>"1328982632", "kkl"=>"fgn fgn", "title"=>" fgn" ) ) ), array("nm"=>"nm", "type"=>"nwsfd", "kkl"=>"nws fd", "nws"=>array(array("date"=>"1328982655", "kkl"=>" gh fgh","title"=>"n fgh n" ) ) ) ) ) ); function test_print(&$item, $key, $pages) { if($key == 'date') { //strip the slashes $item = stripslashes($item); //if the key is 'date' which is deeply nested, return the key. echo key(current($pages)); echo '<br/>'; } } array_walk_recursive($pages, 'test_print', $pages); ?> What is returned from the 'test_print' function is: Quote variation variation variation Is this because array_walk_recursive() does not move the internal pointer pointer along? I found this thread on stackoverflow: http://stackoverflow.com/questions/3133054/php-find-key-position-in-multidimensional-array which has a great function for finding the key, however i'm struggling to get my head around how I can apply something like this whilst using array_walk_recursive(). Does anyone have any ideas? Hi I am having trouble using the min() function to find the lowest value within my session arrays. On my site i have the following car values saved into my $car array: Code: [Select] $car = array( "itemNumber" => $itemNumber, "Model" => $Model, "EngineSize" => $EngineSize, "Colour" => $Colour, "Value" => $Value, ); I then store each $item array into a $_SESSION['all'] as below: Code: [Select] if (isset($_SESSION['all'])) { $_SESSION['all'][$number] = $car; } else { $_SESSION['all'] = array($number => $car); } Now i need to find the lowest Value of all the cars that the user may add. I have been using the min() function to try and do this with the following code but it gives me the error: Wrong parameter count for min()..... Code: [Select] $cheapest = min($car['Value']); Am i on the right tracks or is this not possible? If anyone could shed some light on how i can get this to work, would be very much appreciated! Hi all, Just a pretty basic question that I can't find the asnwer to: I have a MySQL query of the following form: Code: [Select] $result = mysql_query (" SELECT $cols FROM tablename WHERE......."); Where $cols is as follows: Code: [Select] $cols = firstname, lastname, $price1, $price2, etc. ($price1 and $price2 are variable column names that contains prices) How do I find the lowest value for $price1 and/or $price2 out of the results set of this query? Thanks! |