|
|
Subject:
php array_multisort() function: please help me understand the syntax
Category: Computers > Programming Asked by: phoogoo-ga List Price: $10.00 |
Posted:
18 Sep 2004 18:22 PDT
Expires: 18 Oct 2004 18:22 PDT Question ID: 403062 |
|
There is no answer at this time. |
|
Subject:
Re: php array_multisort() function: please help me understand the syntax
From: crythias-ga on 18 Sep 2004 22:22 PDT |
Just wanted to tell you that your program does exactly what you asked, I think, but you might want to SORT_DESC to get arrayID1 first... http://us2.php.net/manual/en/function.array-multisort.php This is a free comment. |
Subject:
Re: php array_multisort() function: please help me understand the syntax
From: phoogoo-ga on 19 Sep 2004 08:42 PDT |
It looks like it's working, but it's sorting them in that order because of the varying number of elemements in each secondary array. I changed the number of elements so you can see it better. <?php $myArray = array( array ("arrayID1", "a1", "a3", "a5", "a2", "a4", "a6"), array("arrayID3", "b1", "b3", "b4", "b2"), array("arrayID2", "c1", "c3", "c5", "c2", "c4", "c5", "c7", "c7")); echo "<pre>"; print_r($myArray); echo "</pre>"; array_multisort($myArray, SORT_ASC); echo "<pre>"; print_r($myArray); echo "</pre>"; ?> Here it what it prints: Array ( [0] => Array ( [0] => arrayID1 [1] => a1 [2] => a3 [3] => a5 [4] => a2 [5] => a4 [6] => a6 ) [1] => Array ( [0] => arrayID3 [1] => b1 [2] => b3 [3] => b4 [4] => b2 ) [2] => Array ( [0] => arrayID2 [1] => c1 [2] => c3 [3] => c5 [4] => c2 [5] => c4 [6] => c5 [7] => c7 [8] => c7 ) ) Array ( [0] => Array ( [0] => arrayID3 [1] => b1 [2] => b3 [3] => b4 [4] => b2 ) [1] => Array ( [0] => arrayID1 [1] => a1 [2] => a3 [3] => a5 [4] => a2 [5] => a4 [6] => a6 ) [2] => Array ( [0] => arrayID2 [1] => c1 [2] => c3 [3] => c5 [4] => c2 [5] => c4 [6] => c5 [7] => c7 [8] => c7 ) ) |
Subject:
Re: php array_multisort() function: please help me understand the syntax
From: crythias-ga on 19 Sep 2004 11:54 PDT |
Using the comment on the link, I revised the code... <?php // $arr = array to sort. // $col = column to sort by. function incision_sort($arr, $col){ for($k = 0; $k < sizeof($arr)-1; $k++){ // $arr[$k+1] is possibly in the wrong place. Take it out. $t = $arr[$k+1]; $i = $k; // Push $arr[i] to the right until we find the right place for $t. while($i >= 0 && $arr[$i][$col] > $t[$col]){ $arr[$i+1] = $arr[$i]; $i--; } // Insert $t into the right place. $arr[$i+1] = $t; }// End sort return $arr; } $myArray = array( array ("arrayID1", "a1", "a3", "a5", "a2", "a4", "a6"), array("arrayID3", "b1", "b3", "b4", "b2"), array("arrayID2", "c1", "c3", "c5", "c2", "c4", "c 5", "c7", "c7")); echo "<pre>"; print_r($myArray); echo "</pre>"; //array_multisort($myArray, SORT_DESC); //echo "<pre>"; //print_r($myArray); //echo "</pre>"; $sortarray=incision_sort($myArray,0); echo "sort2<pre>"; print_r($sortarray); echo "</pre>"; ?> Result: Array ( [0] => Array ( [0] => arrayID1 [1] => a1 [2] => a3 [3] => a5 [4] => a2 [5] => a4 [6] => a6 ) [1] => Array ( [0] => arrayID3 [1] => b1 [2] => b3 [3] => b4 [4] => b2 ) [2] => Array ( [0] => arrayID2 [1] => c1 [2] => c3 [3] => c5 [4] => c2 [5] => c4 [6] => c5 [7] => c7 [8] => c7 ) ) sort2 Array ( [0] => Array ( [0] => arrayID1 [1] => a1 [2] => a3 [3] => a5 [4] => a2 [5] => a4 [6] => a6 ) [1] => Array ( [0] => arrayID2 [1] => c1 [2] => c3 [3] => c5 [4] => c2 [5] => c4 [6] => c5 [7] => c7 [8] => c7 ) [2] => Array ( [0] => arrayID3 [1] => b1 [2] => b3 [3] => b4 [4] => b2 ) ) |
Subject:
Re: php array_multisort() function: please help me understand the syntax
From: phoogoo-ga on 19 Sep 2004 12:56 PDT |
I'm familiar with this function (incision_sort) from the php.net site. It will work for my purposes. However, my question was specifically regarding a very clear explanation of the array_multisort() function syntax as it relates to the array provided and my desired outcome. The examples and explanation on the php.net site, my reference books, etc. don't explain it in a way that I can totally understand. I want a php guru to explain the array_multisort() function syntax in great detail so that my pee brain can fully grasp it. |
Subject:
Re: php array_multisort() function: please help me understand the syntax
From: grimm-ga on 07 Oct 2004 09:17 PDT |
The answer you are looking for is there: http://php-programming-goodies.euweb.cz/how-to-sort-multidimensional-array.html Done it, tried it, it works. You can even sort by multiple field, example: <? ## Sorting by Rating DESC, Title ASC $unsorted_array[] = array("Rating" => 2, "Title" => "Consulate"); $unsorted_array[] = array("Rating" => 1, "Title" => "Fury"); $unsorted_array[] = array("Rating" => 6, "Title" => "Baloon"); $unsorted_array[] = array("Rating" => 3, "Title" => "Appendix"); $unsorted_array[] = array("Rating" => 2, "Title" => "Donkey"); $unsorted_array[] = array("Rating" => 2, "Title" => "Carriage"); ## Print array before sorting print_r($unsorted_array); ## Put values to use for sorting in arrays foreach ($unsorted_array as $array_row) { $sorting_values["Rating"][] = $array_row["Rating"]; $sorting_values["Title"][] = $array_row["Title"]; } ## Sort array using sorting arrays array_multisort($sorting_values["Rating"],SORT_DESC,$sorting_values ["Title"],SORT_ASC,$unsorted_array); ## Print array after sorting print_r($unsorted_array); ?> |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |