Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Iban Dominguez
Iban Dominguez
17,973 Points

Grouping Array by Values

Hi there!

I have an array that displays a list of events sorted by groups,

Here is my Code:

 array(2) {
  [2]=>
  array(1) {
    [0]=>
    array(6) {
      ["date_ref"]=>
      string(19) "2014-04-03 19:00:00"
      ["id"]=>
      string(1) "2"
      ["ref"]=>
      string(0) ""
      ["event_id"]=>
      string(1) "1"
      ["group_id"]=>
      string(1) "2"
      ["fee"]=>
      string(2) "15"
    }
  array(1) {
    [0]=>
    array(6) {
      ["date_ref"]=>
      string(19) "2014-04-28 19:00:00"
      ["id"]=>
      string(1) "5"
      ["ref"]=>
      string(0) ""
      ["event_id"]=>
      string(1) "1"
      ["group_id"]=>
      string(1) "2"
      ["fee"]=>
      string(2) "45"
    }
  }
  [3]=>
  array(3) {
    [0]=>
    array(6) {
      ["date_ref"]=>
      string(19) "2014-04-14 19:00:00"
      ["id"]=>
      string(1) "2"
      ["ref"]=>
      string(0) ""
      ["event_id"]=>
      string(1) "2"
      ["group_id"]=>
      string(1) "3"
      ["fee"]=>
      string(2) "15"
    }
    [1]=>
    array(6) {
      ["date_ref"]=>
      string(19) "2014-04-21 19:00:00"
      ["id"]=>
      string(1) "2"
      ["ref"]=>
      string(0) ""
      ["event_id"]=>
      string(1) "3"
      ["group_id"]=>
      string(1) "3"
      ["fee"]=>
      string(2) "15"
    }
    [2]=>
    array(6) {
      ["date_ref"]=>
      string(19) "2014-04-28 19:00:00"
      ["id"]=>
      string(1) "2"
      ["ref"]=>
      string(0) ""
      ["event_id"]=>
      string(1) "4"
      ["group_id"]=>
      string(1) "3"
      ["fee"]=>
      string(2) "15"
    }
  }
}

I would like to output to the screen placing all events that belongs to same group and have the same fee in one line, example:

date Ref: 2014-04-28 19:00:00, group: 3, single-event-fee: 15, count: 2 events

date Ref: 2014-04-28 19:00:00, group: 3, single-event-fee: 25, count: 3 events

Thank you very much beforehand

3 Answers

Andrew Shook
Andrew Shook
31,709 Points

Use MySQL. So much easier to write a query for that than to write a function in PHP to loop through the array and compare values

Iban Dominguez
Iban Dominguez
17,973 Points

Thanks Andrew,

I got it sorted using GROUP BY for values, GROUP_CONCAT for date strings, COUNT for the amount of grouped events and SUM for the total!

So much easier using mysql! Cant believe I wasted the whole day looping through the array!

Cheers!

Andrew Shook
Andrew Shook
31,709 Points

Glad I could help. I often find it useful to run idea's by people and get a fresh perspective on things. Definitely helps prevent over engineering things.

Andrew Shook
Andrew Shook
31,709 Points

Use MySQL. So much easier to write a query for that than to write a function in PHP to loop through the array and compare values

Michael Soileau
Michael Soileau
16,199 Points

Just to be thorough here, you can use array_sort and other PHP functions to sort an array. For a multi-indexed array, this is one option.

http://us3.php.net/manual/en/function.array-multisort.php

This can be useful if the information you are pulling isn't from a database (in which case you should use SQL syntax), but if you're pulling from a JSON request, you might have to format it manually.