PHP Array, Dynamic Array Name or Other Technique

I have inherited a project where I need to add data to a table in the following format.

Fitness101,FT101,EA101,RX101

What I am getting in one array is the following:

Fitness101,FT101
Fitness101,EA101
Fitness101,RX101
Fitness102,FT102
Fitness102,EA102
Fitness102,RX102
Fitness103,FT103
Fitness103,EA103
Fitness103,RX103

I need to group the classes for each fitness level and add the following 3 records/rows:

Fitness101,FT101,EA101,RX101
Fitness102,FT102,EA102,RX102
Fitness103,FT103,EA103,RX103

What is the best approach to accomplish this?

I tried several if/while/foreach statements/loops but no luck.

Can a dynamic array name be used or am I am making this more complicated than necessary?

Thanks.

If I print the array it looks like this:

[0] => Fitness101,FT101
[1] => Fitness101,EA101
[2] => Fitness101,RX101
[4] => Fitness102,FT102
[5] => Fitness102,EA102
[6] => Fitness102,RX102
[7] => Fitness103,EA103
[8] => Fitness103,EA103
[9] => Fitness103,RX103


ANSWERS:


Assuming your input is formatted in the following way:

$entries = [
    "Fitness101,FT101",
    "Fitness101,EA101",
    "Fitness101,RX101",
    "Fitness102,FT102",
    "Fitness102,EA102",
    "Fitness102,RX102",
    "Fitness103,FT103",
    "Fitness103,EA103",
    "Fitness103,RX103"
];

$levels = array();
foreach ($entries as $entry) {
    list($level, $class) = explode(',', $entry);
    if (!isset($levels[$level])) {
        $levels[$level] = $level;
    }
    $levels[$level] .= ','.$class;
}
$levels = array_values($levels);

echo json_encode($levels);
// Prints:
// ["Fitness101,FT101,EA101,RX101","Fitness102,FT102,EA102,RX102","Fitness103,FT103,EA103,RX103"]

As usual for this type of grouping problem, you can use the column you wish to group by as a key in your result array. If you are starting with an array in this format:

$your_array = [
    ['Fitness101','FT101'],
    ['Fitness101','EA101'],
    // etc.
];

Then it would work like this:

foreach ($your_array as $row) {
    $result[$row[0]][0] = $row[0];  // write or overwrite the first column
    $result[$row[0]][] = $row[1];   // append the subsequent columns
}

Here $row[0] is the first column with "Fitness101", etc., and when you use it as a key in the $result array, you'll be adding each of the values into a column in a row indicated by that key, so they'll be grouped together as in your desired output.


If you have an array of strings instead, then it's pretty much the same, just explode the row first.

$your_array = [
    'Fitness101,FT101',
    'Fitness101,EA101',
    // etc.
];

foreach ($your_array as $row) {
    $row = explode(',', $row);
    $result[$row[0]][0] = $row[0];
    $result[$row[0]][] = $row[1];
}


 MORE:


 ? PHP Array, Dynamic Array Name or Other Technique
 ? Building a multidimensional array dynamically from another array with PHP?
 ? Counting rows/records in a multidimensional array
 ? Php dynamic array name
 ? Get select option value from forms using jquery created dynamically in loop
 ? Get select option value from forms using jquery created dynamically in loop
 ? Get select option value from forms using jquery created dynamically in loop
 ? How do I create a dynamic form in Drupal 6 using jQuery?
 ? How do I get the data from forms dynamically created with jQuery into php?
 ? Dynamically selecting Dropdown's options