Re: looking for an array sorting function.. just not sure which one.. and how yet

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux