Home > Article > Backend Development > How to Group Array Elements and Combine Values from Another Column in a Multidimensional Array?
Grouping Array Elements by Column and Combining Values from Another Column
Given an array containing nested arrays with two columns, the task is to group the subarrays based on a specific column and concatenate the values from another column within each group, resulting in a comma-separated list.
Consider the following example array:
<code class="php">$array = [ ["444", "0081"], ["449", "0081"], ["451", "0081"], ["455", "2100"], ["469", "2100"] ];</code>
The desired result is:
<code class="php">[ ["444,449,451", "0081"], ["455,469", "2100"] ]</code>
One solution to this problem involves the following steps:
Iterate through the given array, and for each item:
The above is the detailed content of How to Group Array Elements and Combine Values from Another Column in a Multidimensional Array?. For more information, please follow other related articles on the PHP Chinese website!