[PHP] Sorting Multidimensional Array
Hi, RE: Sorting Multidimensional Array I'm trying to sort a multidimensional array. The data was taken from a mysql query: $myrow = mysql_fetch_row($result) { query[] = $myrow; } The purpose is to retrieve the table data and manually add a record, then sort ASC by the startdate which is the forth field... Something like: $test = array_multisort($query, $key = '$query[4]'); Any help would be greatly appreciated. Thanks, Keith -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting Multidimensional Array
Thanks Richard, But wouldn't "order by z" interfere with my original order by values? My code is as follows: $fields1 = " ID, Title, Label, Location, Start, End, Time, "; $fields1 .= " Description, Organization, Department, Contact, "; $fields1 .= " Phone, Email, Global, Board, Committee, Status, TBD_Time "; $command = " SELECT $fields1, TO_DAYS(End) - TO_DAYS(Start) + 1 AS Days "; $command .= " FROM $tablename "; $command .= " $WHERETXT ORDER BY $sortby $sortorder "; I just need to add a new record, but I have to use PHP because the Start and End dates for that record must match the current month... If I could add the record to the Select statement as you suggest that would be wonderful. Keith - Original Message - From: "Richard Lynch" <[EMAIL PROTECTED]> To: "Keith Spiller" <[EMAIL PROTECTED]> Cc: Sent: Tuesday, October 31, 2006 10:48 AM Subject: Re: [PHP] Sorting Multidimensional Array On Tue, October 31, 2006 11:10 am, Keith Spiller wrote: RE: Sorting Multidimensional Array I'm trying to sort a multidimensional array. The data was taken from a mysql query: $myrow = mysql_fetch_row($result) { query[] = $myrow; } The purpose is to retrieve the table data and manually add a record, then sort ASC by the startdate which is the forth field... Something like: $test = array_multisort($query, $key = '$query[4]'); Any help would be greatly appreciated. Thanks, Don't do that. :-) For starters, sorting in PHP is MUCH less efficient than in a DB. Secondly, getting multi-dimensional (sic) arrays in PHP to sort like you want generates a ton of traffic here, so it must be hard. :-) You can do somethig like this: (SELECT x, y, z FROM real_data UNION SELECT 'manually', 'inserted', 'data' as z ) ORDER BY z And achieve MUCH better results with far less headache. If 'z' is indexed in real_data, add another ORDER BY z right before the UNION -- That will probably make it very fast/easy for the DB to splice in your manually-inserted 'z' value. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting Multidimensional Array
On Tue, October 31, 2006 11:10 am, Keith Spiller wrote: > RE: Sorting Multidimensional Array > > I'm trying to sort a multidimensional array. The data was taken from > a mysql query: > > $myrow = mysql_fetch_row($result) { > query[] = $myrow; > } > > The purpose is to retrieve the table data and manually add a record, > then sort ASC by the startdate which is the forth field... > > Something like: > > $test = array_multisort($query, $key = '$query[4]'); > > Any help would be greatly appreciated. Thanks, Don't do that. :-) For starters, sorting in PHP is MUCH less efficient than in a DB. Secondly, getting multi-dimensional (sic) arrays in PHP to sort like you want generates a ton of traffic here, so it must be hard. :-) You can do somethig like this: (SELECT x, y, z FROM real_data UNION SELECT 'manually', 'inserted', 'data' as z ) ORDER BY z And achieve MUCH better results with far less headache. If 'z' is indexed in real_data, add another ORDER BY z right before the UNION -- That will probably make it very fast/easy for the DB to splice in your manually-inserted 'z' value. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multidimensional array by a second level value
> > I'd like to sort the array based on one of the values in the field > > href, description, or time. Is there a canonical way of doing this? you might be able to use http://us2.php.net/manual/en/function.usort.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multidimensional array by a second level value
> I'd like to sort the array based on one of the values in the field > href, description, or time. Is there a canonical way of doing this? Probably easiest to write your own sort function the use usort(), http://www.php.net/usort has pretty good documentation on how to accomplish this. Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multidimensional array by a second level value
http://ca3.php.net/manual/en/function.array-multisort.php this might do it. Jason Chris Lott <[EMAIL PROTECTED]> wrote: > > I have an array $links like this: > > [1] => Array > ( > [href] => http://www.poetrymagazine.org/epstein_sept_prose.html > [description] => Thank You, No > [time] => 2004-09-17T17:30:32Z > ) > > [2] => Array > ( > [href] => http://110am.com/projects/manuscript/ > [description] => Manuscript > [time] => 2004-09-16T19:25:14Z > ) > > I'd like to sort the array based on one of the values in the field > href, description, or time. Is there a canonical way of doing this? > > c > -- > Chris Lott > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sorting multidimensional array by a second level value
I have an array $links like this: [1] => Array ( [href] => http://www.poetrymagazine.org/epstein_sept_prose.html [description] => Thank You, No [time] => 2004-09-17T17:30:32Z ) [2] => Array ( [href] => http://110am.com/projects/manuscript/ [description] => Manuscript [time] => 2004-09-16T19:25:14Z ) I'd like to sort the array based on one of the values in the field href, description, or time. Is there a canonical way of doing this? c -- Chris Lott -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sorting Multidimensional Array..second dimension has 12 elements
I have a 2 dimensional array...with the second dimension having 12 elements. What I want to do is sort (and re-sort) they array by one of the 12 elements...keeping data consistent. All of the examples I have read @php.net seem to deal with multidim-arrays that only have one element in them. Here is a cut-n-paste of a print_r() of my array: -begin paste-- Array ( [barnhij] => Array ( [name] => Jeremy Barnhill [toi] => 5855 [pdk] => 1456 [tpdk_trans] => 40169 [tpallm_trans] => 17430 [terrors] => 174 [total_trans] => 57599 [pct] => 97.03 [modpct] => 98.3630 [pdk_pct] => 69.74 [pallm_pct] => 30.26 [trans_per_req] => 9.84 ) [clarkm10] => Array ( [name] => Michelle Clark [toi] => 1971 [pdk] => 402 [tpdk_trans] => 15427 [tpallm_trans] => 9970 [terrors] => 97 [total_trans] => 25397 [pct] => 95.08 [modpct] => 97.3484 [pdk_pct] => 60.74 [pallm_pct] => 39.26 [trans_per_req] => 12.89 ) [humphrj5] => Array ( [name] => Jill Humphries [toi] => 6104 [pdk] => 1002 [tpdk_trans] => 42759 [tpallm_trans] => 28907 [terrors] => 125 [total_trans] => 71666 [pct] => 97.95 [modpct] => 98.8889 [pdk_pct] => 59.66 [pallm_pct] => 40.34 [trans_per_req] => 11.74 ) [roberj15] => Array ( [name] => Jamison Roberts [toi] => 7132 [pdk] => 1524 [tpdk_trans] => 52542 [tpallm_trans] => 42311 [terrors] => 46 [total_trans] => 94853 [pct] => 99.36 [modpct] => 99.6533 [pdk_pct] => 55.39 [pallm_pct] => 44.61 [trans_per_req] => 13.30 ) ) ---end paste- Hope this is clear, Thanks - Jamison. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]