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

  • Home
  • SEARCH
  • 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 7926533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:48:49+00:00 2026-06-03T18:48:49+00:00

I have the following array and I want to group all the users of

  • 0

I have the following array and I want to group all the users of same company together. Currently every user is shows up with a company id and some times there are 2 or more users under one compay. So I kinda want to revert it and show the users under company not company under users. How’d I go about doing that

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [company_id] => 20
                    [type] => 
                )


        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 6
                    [company_id] => 21
                    [type] => 
                )


        )

    [2] => Array
        (
            [User] => Array
                (
                    [id] => 7
                    [company_id] => 22
                    [type] => 
                )


        )

    [3] => Array
        (
            [User] => Array
                (
                    [id] => 14
                    [company_id] => 21
                    [type] => 
                )


        )

    [4] => Array
        (
            [User] => Array
                (
                    [id] => 15
                    [company_id] => 22
                    [type] => 
                )



        )

    [5] => Array
        (
            [User] => Array
                (
                    [id] => 16
                    [company_id] => 21
                    [type] => 
                )


                )

        )

)
  • 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-03T18:48:50+00:00Added an answer on June 3, 2026 at 6:48 pm

    From the example I suppose that your relations are as follows:

    “User belongsTo Company” and “Company hasMany User”

    @tigrang is right, if you do a find on Company, you will get the users Grouped by Company:

    $this->Company->find('all', array('conditions' => array('id' => $companyId)));
    

    If your Company Model has many other relations and you need only the Users you can use the ContainableBehaviour. Firstly add it in the Company model:

    public $actsAs = array('Containable);
    

    Then in the find() call:

    $this->Company->find('all', array('conditions' => array('id' => $companyId), 'contain' => array('User')));
    

    The ‘contain’ key accepts an array of Model names or a string for a single Model. It will adjust the model bindings on the fly, leaving you only with the Company and User models’ data (if used as in the example above).

    And a little thing I missed. You can also use the ‘group’ key in a find over the User model:

    $this->User->find('all', array('group' => 'company_id'));
    

    Try both approaches and see what you come up with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to see all users which following me or another user. I have
I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']
I have the following array $group= array( [0] => 'apple', [1] => 'orange', [2]
I have the following segment of code in which I want to Group everything
I want to group by every minute between date1 and date2 in the following
I have the following code. I want to add/remove check-box values to an array
I have following two arrays. I want the difference between these two arrays. That
I have following array, that I need to operate by hand on bitmaps. const
I have the following array: $franchise_a_status[] = array( 'id' => $franchise['franchise_id'], 'text' => $franchise['franchise_surname'].
I have the following array(in python): arr= ['\n', ' a#B\n', ' c#D\n'] Now I

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.