[PHP] Re: looking for an array sorting function.. just not sure which one.. and how yet
On 03/10/2012 06:42 PM, Govinda wrote: Hi Everyone I am newbie enough with the terminology around PHP arrays that I am slow to wrap my head around what I know is in the docs (what i am reading)... but just can't identify yet. I think it will be faster if someone can translate for me in terms of my question: I make an array by building up its members form the results of a db call, like so: foreach ($query-result() as $row) { $arr_cTree[$row-cID] = array($row-cPcID, $row-cL, $row-cName, $row-cSeg, $row-cSort); } I have that array's members' values displaying on an HTML page fine by making recursive function calls to grab the children of each parent member found.. but the order those array members are displayed on the page is not what I want (it seems to be displaying according to the order the rows are found in the db). To remedy, I want to sort on the last value in the array which is the value of each array member. (If I said that right.) IOW, I want to sort my $arr_cTree array by the decimal values stored in $row-cSort ($arr_cTree[[4]), before I then display on the page. Can someone point me to the array sorting function you think is suited to this job.. and perhaps give me an example in pseudo code if that helps make it more clear in this case. Thanks! -Govinda The best option is to sort it in the query. But if for whatever reason you sort it in the query for use one way and then need it sorted differently later, then most likely execute another query with the new sorting. If having tried that, and that's not what works, then use array_multisort(). Pay attention to example #3 on php.net as this is what you want. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: looking for an array sorting function.. just not sure which one..and how yet
On 03/10/2012 07:01 PM, Shawn McKenzie wrote: foreach ($query-result() as $row) { $arr_cTree[$row-cID] = array($row-cPcID, $row-cL, $row-cName, $row-cSeg, $row-cSort); } Actually, you may construct your array as follows to make sorting with ksort(): foreach ($query-result() as $row) { $arr_cTree[$row-cSort] = array[$row-cID, $row-cPcID, $row-cL, $row-cName, $row-cSeg, $row-cSort); } However you do it, look at constructing your array the way you need it to easily sort, display etc. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: looking for an array sorting function.. just not sure which one.. and how yet
The best option is to sort it in the query. yes, of course!, that makes sense.. I feel silly not thinking of that in the first place. Now I do not need to sort the array with PHP.. but glad you mentioned array_multisort() as well.. so I could make a mental note of that function. Thanks to Stephen too for your reply. I appreciate the points you made.. and why, I believe. (Even if I am not heeding them all just now) Thanks guys! -Govinda -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php