twistedindustries Posted May 10, 2011 Share Posted May 10, 2011 I have been struggling with this for awhile now, and I think it isn't as hard as I making it. Basically I want to "group" my array by month making a multidimensional array by months. Basically right now my print_r($my_array) outputs: Array ( [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [5] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) I need to take that and make it look like this, grouping by the month of the 'dates' value: Array ( [0] => Array ( //June [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) [1] => Array ( //July [0] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [plz] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/ Share on other sites More sharing options...
Psycho Posted May 10, 2011 Share Posted May 10, 2011 Where are you getting these values from? If you are getting from a DB you should order the records appropriately there. Although, there is usually no need to physically group them in an array since you can handle that in the processing code. But, if you really need to do this, the only way I can see to do it is to loop over each record and create a new array. Also, I would set the key for the "grouped" elements to the month number for ease of use. $groupedArray = array(); foreach($originalArray as $key => $valuesAry) { $month = date('n', strtotime($valuesAry['dates'])); $groupedArray[$month][] = $valuesAry; } //$groupedArray now holds the data from $originalArray in a grouped format Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213469 Share on other sites More sharing options...
twistedindustries Posted May 10, 2011 Author Share Posted May 10, 2011 Thank you for your help but after trying this code: $originalArray = $this->_data; $groupedArray = array(); foreach($originalArray as $key => $valuesAry) { $month = date('n', strtotime($valuesAry['dates'])); $groupedArray[$month][] = $valuesAry; } I get this error: Fatal error: Cannot use object of type stdClass as array It says that it is coming from this line: $month = date('n', strtotime($valuesAry['dates'])); Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213475 Share on other sites More sharing options...
twistedindustries Posted May 10, 2011 Author Share Posted May 10, 2011 Aha! The line in question needed to look like this: $month = date('n', strtotime($valuesAry->dates)); I think I have it now! Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213476 Share on other sites More sharing options...
twistedindustries Posted May 10, 2011 Author Share Posted May 10, 2011 Where are you getting these values from? If you are getting from a DB you should order the records appropriately there. Although, there is usually no need to physically group them in an array since you can handle that in the processing code. But, if you really need to do this, the only way I can see to do it is to loop over each record and create a new array. Also, I would set the key for the "grouped" elements to the month number for ease of use. $groupedArray = array(); foreach($originalArray as $key => $valuesAry) { $month = date('n', strtotime($valuesAry['dates'])); $groupedArray[$month][] = $valuesAry; } //$groupedArray now holds the data from $originalArray in a grouped format Quick question, what if instead of using the month number as the key I just wanted to start the key at 0 and have it increase for each month? Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213479 Share on other sites More sharing options...
twistedindustries Posted May 10, 2011 Author Share Posted May 10, 2011 I wish this form had an edit button, I have worked through my issue and I came to the conclusion that you are right and that I should probably try and accomplish what I need directly from the database. The problem is I don't know how. Can someone post someecode to help me? Here is my query as it is: SELECT a.id, a.id AS did, a.dates, a.enddates, a.shortdescription, a.max_attendance, a.times, a.endtimes, a.registra, a.unregistra, a.titel, a.locid, a.status,a.shw_attendees, l.club, l.url, l.street, l.plz, l.city, l.country, l.locdescription, c.catname, c.id AS catid FROM #__registrationpro_dates AS a LEFT JOIN #__registrationpro_locate AS l ON l.id = a.locid LEFT JOIN #__registrationpro_categories AS c ON c.id = a.catsid WHERE a.published = 1 AND c.access <= 0 ORDER BY c.ordering, a.dates, a.times Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213493 Share on other sites More sharing options...
Psycho Posted May 10, 2011 Share Posted May 10, 2011 Quick question, what if instead of using the month number as the key I just wanted to start the key at 0 and have it increase for each month? That makes the problem more complex, because now you will need to keep track of which month belongs to which index. In your example above "2011-06-01" is the date for the first record so you would make index 0 to represent "May". Now, when you get to the 2nd record and it has a date of "2011-06-05" how would the code "know" that the index of 0 already exists and that is represents the month of May. You could do it, but it would require creating another array to keep track of the month/index association and additional logic to check of the month of the current record is already in that array or if you need to create one. So, yes it can be done, but you would be making much more complicated than it should be $groupedArray = array(); $monthsArray = array(); foreach($originalArray as $key => $valuesAry) { //Get month of current record $month = date('n', strtotime($valuesAry['dates'])); //Check if month exists in index array if(!in_array($month, $monthsArray)) { //Month does not exist in index array add it $monthsArray[] = $month; } //Get month index value for current month $monthIndex = array_search($month, $monthsArray)); //Add record to new array under month index $groupedArray[$monthIndex][] = $valuesAry; } Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213494 Share on other sites More sharing options...
Psycho Posted May 10, 2011 Share Posted May 10, 2011 I wish this form had an edit button, I have worked through my issue and I came to the conclusion that you are right and that I should probably try and accomplish what I need directly from the database. The problem is I don't know how. Can someone post someecode to help me? Here is my query as it is: SELECT a.id, a.id AS did, a.dates, a.enddates, a.shortdescription, a.max_attendance, a.times, a.endtimes, a.registra, a.unregistra, a.titel, a.locid, a.status,a.shw_attendees, l.club, l.url, l.street, l.please, l.city, l.country, l.locdescription, c.catname, c.id AS catid FROM #__registrationpro_dates AS a LEFT JOIN #__registrationpro_locate AS l ON l.id = a.locid LEFT JOIN #__registrationpro_categories AS c ON c.id = a.catsid WHERE a.published = 1 AND c.access <= 0 ORDER BY c.ordering, a.dates, a.times There IS an edit button, but it is only available for about 20 minutes after making a post. I can help you, but the problem you need to resolve is NOT the actual query, but how you process the query. You already have an ORDER BY clause and I will assume it is in the correct order that you will present the values. Here is some rough code to show one way to display the records in a "grouped" format. I only added the output for some of the fields since I am lazy. <?php //Run query to get records returned in proper order $query = "SELECT a.id, a.id AS did, a.dates, a.enddates, a.shortdescription, a.max_attendance, a.times, a.endtimes, a.registra, a.unregistra, a.titel, a.locid, a.status, a.shw_attendees, l.club, l.url, l.street, l.please, l.city, l.country, l.locdescription, c.catname, c.id AS catid FROM #__registrationpro_dates AS a LEFT JOIN #__registrationpro_locate AS l ON l.id = a.locid LEFT JOIN #__registrationpro_categories AS c ON c.id = a.catsid WHERE a.published = 1 AND c.access <= 0 ORDER BY c.ordering, a.dates, a.times "; $result = mysql_query($query) or die(mysql_error()); //Create output for the records grouped by month/year $output = ''; //Variable to store HTML output $currentMonthYear = false; //Flag to detect when month/year changes between records while($row = mysql_fetch_assoc($result)) { //Get MM-YYYY value for current record $thisMonthYear = date('F-Y', $row['dates']); //If this Month/Year different than current Month/Year create new header if($currentMonthYear != $thisMonthYear) { $currentMonthYear = $thisMonthYear; $output .= "<tr><th colspan=''><h1>Records for {$currentMonthYear}</h1><th></tr>\n"; $output .= "<tr>\n"; $output .= "<tr><th>Description</th></tr>\n"; $output .= "<tr><th>Max Attendance</th></tr>\n"; $output .= "<tr><th>Times</th></tr>\n"; $output .= "<tr><th>End Times</th></tr>\n"; $output .= "<tr><th>Club</th></tr>\n"; $output .= "<tr><th>City</th></tr>\n"; $output .= "<tr><th>Country</th></tr>\n"; $output .= "<tr><th>Description</th></tr>\n"; $output .= "</tr>\n"; } //Display current record $output .= "<tr>\n"; $output .= "<td>{$row['shortdescription']}</td>\n"; $output .= "<td>{$row['max_attendance']}</td>\n"; $output .= "<td>{$row['times']}</td>\n"; $output .= "<td>{$row['endtimes']}</td>\n"; $output .= "<td>{$row['club']}</td>\n"; $output .= "<td>{$row['city']}</td>\n"; $output .= "<td>{$row['country']}</td>\n"; $output .= "<td>{$row['locdescription']}</td>\n"; $output .= "</tr>\n"; } ?> <table> <?php echo $output; ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/236041-php-array-group-by-month/#findComment-1213498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.