Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:53:32+00:00 2026-06-10T04:53:32+00:00

I want to group the data in an array with associative rows. Group should

  • 0

I want to group the data in an array with associative rows. Group should be determined by the type value and all label_id values within each group should be formed into a subarray.

Sample input:

$array = [
    ['type' => 'AAA', 'label_id' => 'A1,35'],
    ['type' => 'AAA', 'label_id' => 'A2,34'],
    ['type' => 'BBB', 'label_id' => 'B1,29'],
    ['type' => 'CCC', 'label_id' => 'C1,20'],
    ['type' => 'CCC', 'label_id' => 'C2,19'],
    ['type' => 'CCC', 'label_id' => 'C3,18']
];

The desired result:

[
    [
        'type' => 'AAA',
        'label_id' => [
            'A1,35',
            'A2,34'
        ],
    [
        'type' => 'BBB',
        'label_id' => [
            'B1,29'
        ],
    ],
    [
        'type' => 'CCC',
        'label_id' => [
            'C1,20',
            'C2,19',
            'C3,18',
        ]
    ]
]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:53:34+00:00Added an answer on June 10, 2026 at 4:53 am

    This should do the trick

    $args = array
    (
        array( 'type' => 'AAA', 'label_id' => 'A1,35' ),
        array( 'type' => 'AAA', 'label_id' => 'A2,34' ),
        array( 'type' => 'BBB', 'label_id' => 'B1,29' ),
        array( 'type' => 'CCC', 'label_id' => 'C1,20' ),
        array( 'type' => 'CCC', 'label_id' => 'C2,19' ),
        array( 'type' => 'CCC', 'label_id' => 'C3,18' )  
    );
    
    $tmp = array();
    
    foreach($args as $arg)
    {
        $tmp[$arg['type']][] = $arg['label_id'];
    }
    
    $output = array();
    
    foreach($tmp as $type => $labels)
    {
        $output[] = array(
            'type' => $type,
            'label_id' => $labels
        );
    }
    
    var_dump($output);
    

    The output is :

    array
      0 => 
        array
          'type' => string 'AAA' (length=3)
          'label_id' => 
            array
              0 => string 'A1,35' (length=5)
              1 => string 'A2,34' (length=5)
      1 => 
        array
          'type' => string 'BBB' (length=3)
          'label_id' => 
            array
              0 => string 'B1,29' (length=5)
      2 => 
        array
          'type' => string 'CCC' (length=3)
          'label_id' => 
            array
              0 => string 'C1,20' (length=5)
              1 => string 'C2,19' (length=5)
              2 => string 'C3,18' (length=5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement some code in excel macro to group some data for
I have some data that has various attributes and I want to hierarchically group
I want to be able to search all data based on multiple or conditions
A coworker left some data files I want to analyze with Numpy. Each file
Say you have columns AppleType, CreationDate and want to order each group of AppleType
I made a data.plist and want to load the root array (names) into a
I want to load the list of the groups as well as data into
I have this scenario. I want group on Country and Category. A store can
I want to group an sql statement by a column alias. In essense, I
I want to group every date that it is in the database and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.