user826855
user826855

Reputation: 598

Group row data from a 2d array by one column and accumulate values from another column within each group

I have the following array...

[0] => Array
(
  [Name] => Steve Richard
  [Car_Colour] => Red
)

[1] => Array
(
  [Name] => Scott Lucas
  [Car_Colour] => Orange
)

[2] => Array
(
  [Name] => Danny Sisum
  [Car_Colour] => Red
)

[3] => Array
(
  [Name] => Alice Small
  [Car_Colour] => Green
)

How do I output the data in the array, but group the data by Car Colour, so the people with the two red cars will appear underneath a header of red, and the others for Orange and green?

Example:

People with **Red** cars

    Name:** Steve Richard
    Car Colour:** Red

    Name:** Danny Sisum
    Car Colour: Red

People with **Orange** cars

    Name:** Scott Lucas
    Car Colour:** Orange

People with **Green** cars

    Name:** Alice Small
    Car Colour:** Green

Upvotes: 3

Views: 2313

Answers (4)

Stefan
Stefan

Reputation: 3900

$arr_color = array();  // This line is only truly neccessary if $arr_data can be empty
foreach ($arr_data as $arr) 
    $arr_color[$arr["Car_Colour"]][] = $arr["Name"];

// Display
foreach ($arr_color as $color => $lst_persons) {
    echo "<p>People with *** $color *** cars</p>";
    foreach ($lst_persons as $person) {
        echo "Name: " . $person . "<br>";
    }
}

Input:

  $arr_data = array(
  0 => array(
         "Name" => "Steve Richard",
         "Car_Colour" => "Red"
       ),
  1 => array(
         "Name" => "Scott Lucas",
         "Car_Colour" => "Orange"
       ),
  2 => array(
         "Name" => "Danny Sisum",
         "Car_Colour" => "Red"
       ),
  3 => array(
         "Name" => "Alice Small",
         "Car_Colour" => "Green"
       )
  );

Output:

People with *** Red *** cars
Name: Steve Richard
Name: Danny Sisum

People with *** Orange *** cars
Name: Scott Lucas

People with *** Green *** cars
Name: Alice Small

Upvotes: 0

Baba
Baba

Reputation: 95101

You can try

$array = array(
        "0" => Array("Name" => "Steve Richard","Car_Colour" => "Red"),
        "1" => Array("Name" => "Scott Lucas","Car_Colour" => "Orange"),
        "2" => Array("Name" => "Danny Sisum","Car_Colour" => "Red"),
        "3" => Array("Name" => "Alice Small","Car_Colour" => "Green"));

$list = array();
foreach($array as $data)
{
    $list[$data['Car_Colour']][] = $data;
}

var_dump($list);

To Display them like above

echo "<pre>";
foreach($list as $color => $data)
{
    printf("People with **%s** cars\n",$color);

    foreach($data as $info)
    {
        printf("\tName:** %s\n",$info['Name']);
        printf("\t Car Colour:** %s\n",$info['Car_Colour']);
    }
    print(PHP_EOL);
}

Output

People with **Red** cars
    Name:** Steve Richard
     Car Colour:** Red
    Name:** Danny Sisum
     Car Colour:** Red

People with **Orange** cars
    Name:** Scott Lucas
     Car Colour:** Orange

People with **Green** cars
    Name:** Alice Small
     Car Colour:** Green

Upvotes: 2

Rick Burgess
Rick Burgess

Reputation: 725

something like:

$grouped_array = array();
foreach($array as $person){
   if(!isset($grouped_array[$person['car_colour'])){
         $grouped_array[$person['car_colour']] = array();
   }
   $grouped_array[$person['car_colour'][] = $person['name']; 
}

Upvotes: 1

Sergey Eremin
Sergey Eremin

Reputation: 11080

$array = array(); // The array you currently have
// Grouping
$grouped = array();
foreach ($array as $item) {
    $colour = $item['Car_Colour'];
    if (!isset($grouped[$colour])) {
        $grouped[$colour] = array();
    }
    $grouped[$colour][] = $item['Name'];
}
// Output
$output = '';
foreach ($grouped as $colour => $names) {
    $output .= 'People with **' . $colour . '** cars' . "\n\n";
    foreach ($names as $name) {
        $output .= "\t" . 'Name:** ' . $name . "\n";
        $output .= "\t" . 'Car Colour:** ' . $colour . "\n";
    }
}
echo $output;

Upvotes: 1

Related Questions