zaphodbeeb Posted December 4, 2008 Share Posted December 4, 2008 Folks, I have an array (see sample print_r output below): (Incidentally, it does say [ 0 ] => Array in the first line - something weird changing it to a dot.. [pre]Array ( [0] => Array ( [sdate] => 2454801 [edate] => 2454809 [stime] => 19:30:00 [etime] => 22:00:00 [etype] => Once [etitle] => The Playboy of the Western World [ecategory] => 0[24X5[110X [id] => 107 ) [1] => Array ( [sdate] => 2454801 [edate] => 2454821 [stime] => 20:00:00 [etime] => 22:30:00 [etype] => Once [etitle] => THE REAL THING by Tom Stoppard [ecategory] => 0[24X5[110X [id] => 109 ) [2] => Array ( [sdate] => 2454812 [edate] => 0 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Days [etitle] => Day repeats [ecategory] => 0[21X5[123X [id] => 127 ) [3] => Array ( [sdate] => 2454815 [edate] => 2454815 [stime] => 02:00:00 [etime] => 06:00:00 [etype] => Once [etitle] => Dingle Christmas Carols [ecategory] => 0[12X5[113X [id] => 106 )[/pre] [Array is called $bna by the way] I want to sort by id.... so I'm trying: foreach ($bna as $key => $row) { $sdate[$key] = $row['$sdate']; $edate[$key] = $row['$edate']; $stime[$key] = $row['$stime']; $etime[$key] = $row['$etime']; $etype[$key] = $row['$etype']; $etitle[$key] = $row['$etitle']; $ecategory[$key] = $row['$ecategory']; $id[$key] = $row['$id']; } array_multisort($sdate, $edate, $stime, $etime, $etype, $etitle, $ecategory, $id, SORT_NUMERIC, $bna); print_r($bna); The array is output but not in ID order.... HELP! Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/ Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 foreach($bna as $key => $row){ $id[] = $row['id']; } array_multisort($id, $bna, SORT_NUMERIC); That was just quick, might work :s also put your code in [ code ] [ / code ] That will stop it being reformatted Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705578 Share on other sites More sharing options...
zaphodbeeb Posted December 4, 2008 Author Share Posted December 4, 2008 Doesn't seem to be working... just confirming that so don't re-check just yet [and thanks for the help]. Oddly, I think I may have found a solution (which I'll post shortly) but I don't quite understand *why* it works. Some may say "Who cares...." but I do... Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705585 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 I've run it locally and it worked for me Array ( [0] => Array ( [sdate] => 2454815 [edate] => 2454815 [stime] => 02:00:00 [etime] => 06:00:00 [etype] => Once [etitle] => Dingle Christmas Carols [ecategory] => 0[12X5[113X [id] => 106 ) [1] => Array ( [sdate] => 2454801 [edate] => 2454809 [stime] => 19:30:00 [etime] => 22:00:00 [etype] => Once [etitle] => The Playboy of the Western World [ecategory] => 0[24X5[110X [id] => 107 ) [2] => Array ( [sdate] => 2454801 [edate] => 2454821 [stime] => 20:00:00 [etime] => 22:30:00 [etype] => Once [etitle] => THE REAL THING by Tom Stoppard [ecategory] => 0[24X5[110X [id] => 109 ) [3] => Array ( [sdate] => 2454812 [edate] => 0 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Days [etitle] => Day repeats [ecategory] => 0[21X5[123X [id] => 127 ) ) Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705586 Share on other sites More sharing options...
zaphodbeeb Posted December 4, 2008 Author Share Posted December 4, 2008 Ok, Will try again... and get back in a two minutes... Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705587 Share on other sites More sharing options...
zaphodbeeb Posted December 4, 2008 Author Share Posted December 4, 2008 Ok, here's the code for my array $bna[] = array('sdate' => "2454815", 'edate' => "2454815", 'stime' => "02:00:00", 'etime' => "06:00:00", 'etype' => "Once",'etitle' => "Dingle Christmas Carols ", 'ecategory' => "0[12X5[113X" , 'id' => "106"); $bna[] = array('sdate' => "2454801", 'edate' => "2454809", 'stime' => "19:30:00", 'etime' => "22:00:00",'etype' => "Once", 'etitle' => "The Playboy of the Western World -- new version by Bisi Adigun and Roddy Doyle", 'ecategory' => "0[24X5[110X", 'id' => "107"); $bna[] = array('sdate' => "2454846", 'edate' => "2454870", 'stime' => "20:00:00", 'etime' => "22:30:00", 'etype' => "Once", 'etitle' => "La Dispute, by Pierre Marivaux ", 'ecategory' => "0[24X5[110X", 'id' => "108"); $bna[] = array('sdate' => "2454801", 'edate' => "2454821", 'stime' => "20:00:00", 'etime' => "22:30:00", 'etype' => "Once", 'etitle' => "THE REAL THING by Tom Stoppard", 'ecategory' => "0[24X5[110X", 'id' => "109"); $bna[] = array('sdate' => "2454812", 'edate' => "0", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Days", 'etitle' => "Day repeats ", 'ecategory' => "0[21X5[123X", 'id' => "127"); $bna[] = array('sdate' => "2454892", 'edate' => "0", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Months", 'etitle' => "HTML EVENT ", 'ecategory' => "0[12X5[109X", 'id' => "118"); $bna[] = array('sdate' => "2455817", 'edate' => "2455821", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Months", 'etitle' => "Repeating monthly from 12 dec (12m) ", 'ecategory' => "0[17X5[112X", 'id' => "125"); $bna[] = array('sdate' => "2454826", 'edate' => "2454827", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2455038", 'edate' => "2455039", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2455253", 'edate' => "2455254", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2455465", 'edate' => "2455466", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2455677", 'edate' => "2455678", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2455891", 'edate' => "2455892", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2456104", 'edate' => "2456105", 'stime' => "00:00:00", 'etime' => "00:00:00", 'etype' => "Floating", 'etitle' => "Float Special ", 'ecategory' => "0[25X5[118X", 'id' => "124"); $bna[] = array('sdate' => "2454838", 'edate' => "0", 'stime' => "21:00:00", 'etime' => "22:30:00", 'etype' => "Weeks", 'etitle' => "Weekly ", 'ecategory' => "0[20X5[107X", 'id' => "123"); Here's the code for my loop and using print_r to output to screen to analyse: foreach ($bna as $key => $row) { $id[] = $row['id']; $sdate[$key] = $row['$sdate']; $edate[$key] = $row['$edate']; $stime[$key] = $row['$stime']; $etime[$key] = $row['$etime']; $etype[$key] = $row['$etype']; $etitle[$key] = $row['$etitle']; $ecategory[$key] = $row['$ecategory']; $id[$key] = $row['$id']; } array_multisort($id, $bna, SORT_NUMERIC); And finally here's the output to screen: Array ( [0] => Array ( [sdate] => 2455465 [edate] => 2455466 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [1] => Array ( [sdate] => 2455253 [edate] => 2455254 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [2] => Array ( [sdate] => 2455677 [edate] => 2455678 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [3] => Array ( [sdate] => 2455891 [edate] => 2455892 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [4] => Array ( [sdate] => 2454838 [edate] => 0 [stime] => 21:00:00 [etime] => 22:30:00 [etype] => Weeks [etitle] => Weekly [ecategory] => 0[20X5[107X [id] => 123 ) [5] => Array ( [sdate] => 2456104 [edate] => 2456105 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [6] => Array ( [sdate] => 2455038 [edate] => 2455039 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [7] => Array ( [sdate] => 2454826 [edate] => 2454827 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Floating [etitle] => Float Special [ecategory] => 0[25X5[118X [id] => 124 ) [8] => Array ( [sdate] => 2454846 [edate] => 2454870 [stime] => 20:00:00 [etime] => 22:30:00 [etype] => Once [etitle] => La Dispute, by Pierre Marivaux [ecategory] => 0[24X5[110X [id] => 108 ) [9] => Array ( [sdate] => 2454801 [edate] => 2454809 [stime] => 19:30:00 [etime] => 22:00:00 [etype] => Once [etitle] => The Playboy of the Western World -- new version by Bisi Adigun and Roddy Doyle [ecategory] => 0[24X5[110X [id] => 107 ) [10] => Array ( [sdate] => 2454801 [edate] => 2454821 [stime] => 20:00:00 [etime] => 22:30:00 [etype] => Once [etitle] => THE REAL THING by Tom Stoppard [ecategory] => 0[24X5[110X [id] => 109 ) [11] => Array ( [sdate] => 2454812 [edate] => 0 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Days [etitle] => Day repeats [ecategory] => 0[21X5[123X [id] => 127 ) [12] => Array ( [sdate] => 2455817 [edate] => 2455821 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Months [etitle] => Repeating monthly from 12 dec (12m) [ecategory] => 0[17X5[112X [id] => 125 ) [13] => Array ( [sdate] => 2454892 [edate] => 0 [stime] => 00:00:00 [etime] => 00:00:00 [etype] => Months [etitle] => HTML EVENT [ecategory] => 0[12X5[109X [id] => 118 ) [14] => Array ( [sdate] => 2454815 [edate] => 2454815 [stime] => 02:00:00 [etime] => 06:00:00 [etype] => Once [etitle] => Dingle Christmas Carols [ecategory] => 0[12X5[113X [id] => 106 ) ) Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705588 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 use this to sort it; foreach ($bna as $key => $row) { $id[] = $row['id']; } array_multisort($id, $bna, SORT_NUMERIC); No need to over complecate Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705590 Share on other sites More sharing options...
zaphodbeeb Posted December 4, 2008 Author Share Posted December 4, 2008 Apologies Re-read what you said - and it works fine! Thank you so much. This has been doing my head in for a few hours. The way I was doing it did work with smaller arrays though so I'm a bit lost but your way is MUCH better and simpler to implement. Once again - thanks a million. [is is to early to say Happy Christmas yet?!] Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705597 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Haha, I had to buy my sister a Christmas card to send because she's out in Australia (me in UK) so I'll take a happy Christmas Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705604 Share on other sites More sharing options...
zaphodbeeb Posted December 4, 2008 Author Share Posted December 4, 2008 Re-reading (again, trying to make it stick ) your post. Am thinking that if I added / changed to : foreach ($bna as $key => $row) { $sdate[] = $row['sdate']; $etitle[] = $row['etitle']; } array_multisort($sdate, SORT_ASC, $title, SORT_ASC, $bna); It would then output sorted by sdate (Start Date) first then etitle (Event Title)? Or maybe even array_multisort($sdate, $title, $bna, SORT_ASC); would do it? Link to comment https://forums.phpfreaks.com/topic/135441-having-problems-with-array_multisort/#findComment-705606 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.