PHP - Sort Array By Group
hi,
i'm trying to sort an array of associative arrays, based on one of the keys. if the value of the key is a primitive, normal sorting occurs - this works fine. if the value of the key is an array, i'd like to have it sorted by "groups" here's an example: Code: [Select] <?php $data[] = array('name' => 'h', 'list' => array(1,2)); $data[] = array('name' => 'g', 'list' => array(1)); $data[] = array('name' => 'a', 'list' => array(1,3)); $data[] = array('name' => 'f', 'list' => array(2)); $data[] = array('name' => 'e', 'list' => array(2,3)); $data[] = array('name' => 'b', 'list' => array(3)); $data[] = array('name' => 'c', 'list' => array(1,2,3,4)); $data[] = array('name' => 'd', 'list' => array(3,4)); function deep_sort($array, $sorton){ usort($array, function($a, $b) use($sorton) { $a = $a[$sorton]; $b = $b[$sorton]; if(is_array($a) && is_array($b)){ // this bit is obviously flawed - only included for illustrative purposes $a = implode('', $a); $b = implode('', $b); } return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; }); return $array; } $sorted_by_name = deep_sort($data, 'name'); print '<pre>'; print_r($sorted_by_name); print '</pre>'; $sorted_by_list = deep_sort($data, 'list'); print '<pre>'; print_r($sorted_by_list); print '</pre>'; ?> the sorted_by_name bit is fine - but for sorted_by_list, what i want returned is all the array ordered as follows: all those with "1" (or the lowest if none have "1") in it's list value first, then all those remaining that have the next highest ("2"), etc. so right now, the returns for sort_by_list look like this: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [2] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [3] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [4] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) [7] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) )whereas i'd like to get back: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [2] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) [3] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [4] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [7] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) ) TYIA for any suggestions Similar TutorialsHi All, I hope i'm posting in the right place but also hope someone can help! I've got the following code: <?php $variations = $product->get_available_variations(); if ($variations): ?> <div class="table-responsive"> <table class="table table-striped"> <tbody> <?php foreach ($variations as $key => $value) : ?> <tr> <td><?php echo $value['sku']; ?> <?php echo $value['variation_id']; ?></td> <?php foreach ($value['attributes'] as $attrKey => $attr) : $tax = str_replace('attribute_', '', $attrKey); $term_obj = get_term_by('slug', $attr, $tax); ?> <td><?php echo $term_obj->name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; #$variations?> </tbody> </table> </div><!-- /table-responsive --> <?php endif; #$variations ?> This produces the following table: Product code System Pack Quantity Variation XT1ECWH 234 System 1 2 N/A XT2ECLWH 236 System 2 2 Left XT2ECRWH 237 System 2 2 Right XT3ECWH 238 System 3 2 N/AHowever, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1XT1ECWH 234 Pack Quantity: 2 Variation: N/A System 2 XT2ECLWH 236 Pack Quantity: 2 Variation: Left XT2ECRWH 237 Pack Quantity: 2 Variation: Right System 3 Sorry, not sure why it's not formatted correctly but hopefully you can see what I'm trying to achieve. The array for $variations is as follows: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [sku] => XT1ECWH [variation_description] => [variation_id] => 234 ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => left ) [sku] => XT2ECLWH [variation_description] => Left [variation_id] => 236 ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => right ) [sku] => XT2ECRWH [variation_description] => Right [variation_id] => 237 ) [3] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [price_html] => [sku] => XT3ECWH [variation_description] => ) ) If it helps, I need to always group by the first attribute, in this case : attribute_pa_system Any help on this would be very much appreciated! Thank you in advanced. I have been struggling with this for awhile now, and I think it isn't as hard as I making it. Basically I want to "group" my array by month making a multidimensional array by months. Basically right now my print_r($my_array) outputs: Code: [Select] Array ( [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [5] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) I need to take that and make it look like this, grouping by the month of the 'dates' value: Code: [Select] Array ( [0] => Array ( //June [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) [1] => Array ( //July [0] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) ) Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg Hi All, I've a loop which creates an array as follows: $product_table[] = ['SKU' => $sku, 'Label' => $attribute_name, 'Value' => $term_obj->name ]; I'm then grouping the data by SKU code: #group the products by SKU $group_products = array(); foreach ($product_table as $element) : $group_products[$element['SKU']][] = $element; endforeach; Finally, I output the data: #output the data foreach ($group_products as $itemName => $rows) : echo '<tr>'; #echo '<td>', $element['SKU'], '</td>'; $i=0; foreach ($rows as $row) : $i++; #echo '<td>'. $row["SKU"]. '</td><td>'. $row["Label"]. '</td><td>'. $row["Value"]. '</td>'; if ($i == 1): echo '<td>'. $row["SKU"]. '</td><td>'. $row["Value"]. '</td>'; else: echo '<td>'. $row["Value"]. '</td>'; endif; #echo '<td>'. $row["Value"]. '</td>'; endforeach; echo '</tr>'; endforeach; ?> And looks like: Product code System Pack Quantity XT1CWH System 1 1 x 3m XT2CWH System 2 1 x 3m XT3CWH System 3 1 x 3m
This works perfectly fine. However, some products share the same SKU and therefore it causes an issue, like the below: Product code System Pack Quantity XT1CLWH System 1 8 x 3m System 2 8 x 3m System 3 8 x 3m Is there a way I can avoid this, so if perhaps creates the new row but shows the same SKU code? Many thanks I have it to where it groups two parts of the Array randomly but I need to make it where it won't pair the same together, or if it has already been paired with a team then use another team. Each value of the array can only be used once. Here is my code shuffle($teamname); $loopCount = $games; for ($i = 0; $i < $loopCount; $i++) { $player1 = array_pop($teamname); $player2 = array_pop($teamname); if(is_null($player1)) { echo 'Not enough different teams to create '.$games.' games'; break; } // output to screen echo "Team: " . $player1 . " Vs Team: " . $player2 . "<br />"; } } I have an array that looks like this:
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "2021-02-10 09:06:18" [7]=> string(19) "2021-02-10 09:06:24" }
I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.
Ive been able to produce the following but its not ideal
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "end" [7]=> string(19) "2021-02-10 09:06:18" [8]=> string(19) "2021-02-10 09:06:24" }
$burner_time = array(); foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); } $repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) { array_splice($burner_time, $i++, 0, $repl); } print_r($burner_time); } Hi guys, I'm trying to sort an array. The array has information about listings. ID, name, info etc. An unrelated set of data also contains the listing ID's and I have made a function to sort this array and show an new order for the ID's. I now need to sort the original array by this new ID order... This snippet shows that the original listings array can be sorted by link_id: sort($this->links, $this->link->link_id); //This works. It sorts links by link ID! In a foeach loop I have created this: $sleeps_array[] = array("ID" => $link->link_id, "Sleeps" => $sleeps); // Makes an array of arrays of a custom field with listing ID. It makes this: array ( 0 => array ( 'ID' => '9', 'Sleeps' => '2', ), 1 => array ( 'ID' => '3', 'Sleeps' => '4', ), 2 => array ( 'ID' => '6', 'Sleeps' => '6', ), ) I can then sort this array of arrays with this function: function compare_sleeps($a, $b){ return strnatcmp($a['Sleeps'], $b['Sleeps']); } # sort alphabetically by name usort($sleeps_array, 'compare_sleeps'); It then gives me a sorted array according to "Sleeps" (as in, the number of people a property can cater for). It has the correct order of ID to then output but how do I organise the original array by ID with this new array? Sounds complicated, I hope you got that! Cheers, RJP1 Is there a way to use sort($Array) on numbers but reverse it. So instead of 1 to 3 itll do 3 to 1 ? AKA descending order? Can't find a function that would do unless theres a second sort function ? Thanks Why does krsort return the array as "1"? Code: [Select] $m = array('34' => 1122, '6' => 1944, '9' => 1710); print_r($m); //outputs: Array ( [34] => 1122 [6] => 1944 [9] => 1710 ) print_r(krsort($m)); //outputs: 1 What i need is the output to be Array ( [6] => 1944 [9] => 1710 [34] => 1122) Hi freaks, Got a simple one for ya, I THINK? I have a multi-array that resembles this.. Code: [Select] $_SESSION["book_array"] = array(0 => array("plantID" => $plantID, "botanicalName" => $botanicalName, "commonName" => $commonName, "use" => $use)); I would like to sort it by the botanicalName key of the inner array before I call the forech loop that renders the display so that after it renders the table the items will be seen alphabetically, code below.. Code: [Select] <?php $bookOutput = ""; //$plant_use_array = ''; if (!isset($_SESSION["book_array"]) || count($_SESSION["book_array"]) < 1) { $bookOutput = '<tr><td colspan="4"><h6>Your Book is EMPTY!</h6></td></tr>'; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["book_array"] as $each_item) { $plantID = $each_item['plantID']; $botanicalName = $each_item['botanicalName']; $botanicalName = stripslashes($botanicalName); $commonName = $each_item['commonName']; $commonName = stripslashes($commonName); $use = $each_item['use']; //$x = $i + 1; // Dynamic table row assembly $bookOutput .= "<tr>"; $bookOutput .= '<td><a href="plant_details.php?plantID=' . $plantID . '" id="bodyLink">' . $botanicalName . '</a></td>'; $bookOutput .= '<td>' . $commonName . '</td>'; $bookOutput .= '<td><strong>' . $use . '</strong></td>'; $bookOutput .= '<td><form action="book.php" method="post"><input name="removeBtn" type="submit" value="Remove"/><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $bookOutput .= '</tr>'; $i++; } } ?> Any help would be much appreciated! Thanks in advance. I've tried various sort methods on this array. And seem to be failing. Code: [Select] Array ( [monkey quest] => 8 [monkey] => 2 [monkey sports] => 0 [monkey go happy] => 0 [monkey go happy 3] => 1 [monkey joes] => 0 [monkey games] => 2 [monkey bread recipe] => 1 [monkey go happy 2] => 3 [monkey quest trailer] => 4 [monkey quest guide] => 5 [monkey quest nick] => 6 ) What I want to do or attempt to do rather is sort the array by the => value from highest to lowest (or maybe in reverse as well, but one thing at a time) My last failed attempt I was trying out usort() as my concept.. Code: [Select] function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } echo "<pre>"; print_r(usort($kw, "cmp")); echo "</pre>"; But the only thing I am left with on the page is just 1 everything else seems to be getting lost, or I dunno whats going on. So I think I've done stumped my self. Now I'm looking for idea's assuming I am tackling this all wrong. i need some help sorting my arrays. i have a class that selects data from database, and holds it inside a class to be used multiple times. rather then executing multiple queries. i know how to sort an array. but i am sorting arrays inside an array $data = arrray( array( 'id'=>1, '_name'=>'Server A', '_date_added'=>1298207645.5424, ); array( 'id'=>2, '_name'=>'Server B', '_date_added'=>1298247384.4839, ); array( 'id'=>3, '_name'=>'Server C', '_date_added'=>1298218493.0493, ); ); i want to sort my array descending by key _date_added hope you understand... Hi,
i have an issue to sort gallery photos by date. Right now its sort by Value (Name).
here is the code
index.php contains :
<div> <?php foreach($categories_array as $photo_category=>$photos_array){?> <?php $category_thumbnail = $gallery_url."/layout/pixel.gif"; if(file_exists('files/'.$photo_category.'/thumbnail.jpg')){ $category_thumbnail = $gallery_url.'/'.$photo_category.'/thumbnail.jpg'; } $category_url = $gallery_url.'/'.$photo_category; ?> <span class="category_thumbnail_span" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height+20;?>px;"> <a class="category_thumbnail_image" href="<?php echo $category_url;?>" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height;?>px; background-image:url('<?php echo $gallery_url;?>/layout/lens_48x48.png');" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <img src="<?php echo $category_thumbnail;?>" width="<?php echo $settings_thumbnail_width;?>" height="<?php echo $settings_thumbnail_height;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>" /> </a> <a class="category_thumbnail_title" href="<?php echo $category_url;?>" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 16, '..')), ENT_QUOTES, "UTF-8");?> (<?php echo count($photos_array);?>) </a> </span> <?php } ?> </div>the sort function : ksort($categories_array); I've got a multidimensional array that has 2 columns. The first column is numerical (country_id), and the second column is text (country_name), which may or may not be data that will be turned into an array. Looks something like this: column 1 (index 0) = 42 column 2 (index 1) = Great Britain|England I use a while loop to create an array of these rows, then a use a foreach loop to go through index[1] of each row. If there is a single name for a country, it is added to a new array, but if there is more than one name for a country, then both names are added to the new array. So, for example, if we had this data: row1 column 1 (index 0) = 12 column 2 (index 1) = Algeria row2 column 1 (index 0) = 22 column 2 (index 1) = Ethiopia row3 column 1 (index 0) =42 column 2 (index 1) = Great Britain|England Then the final array would look like this: index[0] = 12 index[1] = Algeria index[0] = 22 index[1] = Ethiopia index[0] = 42 index[1] = Great Britain index[0] = 42 index[1] = England Now I would like to sort by index[1] to get the following alphabetical order: index[0] = 12 index[1] = Algeria index[0] = 42 index[1] = England index[0] = 22 index[1] = Ethiopia index[0] = 42 index[1] = Great Britain What do I use? I've fooled around with sort, usort, asort ... can't figure it out. Thanks in advance. Hello all, I have a rather large array and I need to list it's contents in an alphabetical list. The array is built like this: $array['query']['row']['0']['title'] = 'Bob'; $array['query']['row']['1']['title'] = 'Andy'; $array['query']['row']['2']['title'] = 'Tom'; I need to sort it so it is in alphabetical order based on the title. Is there an easy way to do this? Hi, I have an array of type array_name[roll_number][aggregate_marks]. How do I sort such array on 'aggregate_marks'? My attempts on sort, asort haven't worked. Hello, Im struggling with this, probably because of my lack of fundamental knowledge of arrays. What i want to do is: make an array with two keys. 'id' and 'distance'. I want to sort the results by distance, displaying the id's... im looking at ksort() as the solution, however i don't think im implementing it properly as i dont think i have set the array up properly!! what i have is not good... Code: [Select] <?php while ($row = mysql_fetch_array($result)) { //get the photographers postcode and split it into 2 pieces $p_postcode = explode(' ',$row['address_post_code']); //get the photographers postcode and split it into 2 pieces $u_postcode_a = explode(' ',$u_postcode); //run the two postcodes throught the function to determin the deistance $final_distance = return_distance($u_postcode_a[0], $p_postcode[0]); //start the variable so we can add to it later on $photographers_inrange = ""; //declare this variable to help with the comma spacing too EDIT LOOK TO LINE 46 //$i2 = 1; $photographers_inrange = array(); //if the distance is smaller or equal to the distance the photographer covers if($final_distance <= $row['cover_distance']){ //get their id $photographers_inrange['id'] = $row['ID'].','; $photographers_inrange['distance'] = ''.$final_distance; //echo $photographers_inrange; //EDIT: this method does not work when just one result is returned. now i use substr -1. //if this isnt the last result //if($i++ <= $i2++){ //then add a comma for the sql statement //$photographers_inrange .= ','; //} } ksort($photographers_inrange); foreach ($photographers_inrange as $key => $val) { echo "togs[" . $key . "] = " . $val . "<br/>"; } ?> with $photographers_inrange being where it is, i am getting alot of empty arrays. not what i want. i just need to know how to add to the original array, and how to then sort it. thanks for your ongoing help!! I have a bunch of PDF's, some of them having Uppercase first letters and some having lowercase first letters. Right now my code is alphabetizing all of the Uppercase first letters first then all of the lowercase. I want them to be mixed. $dir = "../../forms/pdf/"; foreach(glob($dir.'*.pdf') as $pdf){ $files[] = str_replace($dir, '', $pdf); } then: <select name="current_pdf" id="current_pdf"> <option value="">Please Select a PDF to replace</option> <?php foreach($files as $file) { echo "<option value=\"$file\">" . $file . "</option>"; } ?> </select> Hello , i am trying to sort a 2d dimensional array by the first column(id) and i havent find anything till now This is my code for the 2d array: $username = $_SESSION['username']; $c = 0; $row=1; $col=1; $users_array = array(); $get_from = mysql_query("SELECT id FROM my_activity WHERE from_user='$username'"); while($fetch1 = mysql_fetch_array($get_from)){ $id[$c] = $fetch1[0]; $get_from_user = mysql_query("SELECT * FROM my_activity WHERE id='$id[$c]'"); $get_user_rows = mysql_num_rows($get_from_user); while($guser = mysql_fetch_array($get_from_user)){ $user[$c] = $guser['to_user']; $users_array[$id[$c]][$user[$c]]= $id[$c].".".$user[$c]; $c++; } } $get_to = mysql_query("SELECT * FROM my_activity WHERE to_user='$username'"); while($fetch2 = mysql_fetch_array($get_to)){ $id[$c] = $fetch2[0]; $get_to_user = mysql_query("SELECT from_user FROM my_activity WHERE id='$id[$c]'"); while($tuser = mysql_fetch_array($get_to_user)){ $user[$c] = $tuser[0]; $users_array[$id[$c]][$user[$c]]= $id[$c].".".$user[$c]; $c++; } } This is what i get if i simply loop the array to get the result: Code: [Select] Array ( [6] => Array ( [checkdate] => 6.checkdate ) [25] => Array ( [asdsadsad] => 25.asdsadsad ) [7] => Array ( [webuser] => 7.webuser ) [9] => Array ( [newuser] => 9.newuser ) [10] => Array ( [bot77un] => 10.bot77un ) ) I am trying to sort it by the ids 6,25,7,9,10 in DESC I have an array I want to define in Php and then sort it. I am pretty new to Php and don't know the syntax or what funcs to use. Here's the array I want: user_email, user_name, hours_worked_per_week, total_earned There will be about 120 users, and I want to sort them ascending alphabetically on user_email |