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 7724697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:45:13+00:00 2026-06-01T04:45:13+00:00

I have one main array which is the returned data from a MySQL query

  • 0

I have one main array which is the returned data from a MySQL query for user table which looks like this :

array(36) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "101"
    ["email"]=>
    string(15) "kkkk@gggg.com"
    ["firstname"]=>
    string(8) "aaaaa"
    ["lastname"]=>
    string(12) "bbbbb"

  }
  [1]=>
  array(4) {
    ["id"]=>
    string(1) "102"
    ["email"]=>
    string(17) "mmmmm@hhhhh.com"
    ["firstname"]=>
    string(12) "vvvv"
    ["lastname"]=>
    string(12) "zzzzz"

  }
  [2]=>
  array(4) {
    ["id"]=>
    string(2) "103"
    ["email"]=>
    string(17) "eeee@gmail.com"
    ["firstname"]=>
    string(6) "ggggg"
    ["lastname"]=>
    string(12) "zzzzz"

And so on…
And a second array which is again MySQL query which is from a table with two foreign keys making relation b/w two table many to many and looks like this:

array(7) {
  [0]=>
  array(2) {
    ["user_id"]=>
    string(3) "101"
    ["group_id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["user_id"]=>
    string(3) "102"
    ["group_id"]=>
    string(1) "1"
  }
  [2]=>
  array(2) {
    ["user_id"]=>
    string(3) "103"
    ["group_id"]=>
    string(1) "1"
  }

The final goal is to make a 5th key in the first array, something like:

[“groups”]=>…

which i can populate with the id’s of the groups which every user takes part in and at the end to look like something like this:

[“groups”]=>”1,2,3,4″

In other words, because the relation is many to many the user may participate in more than one group when I push the new value I don’t want to create a new key with value but instead to concatinate the existing values of the key with the new id.

So far I got something like this:

$query = $this->mUsers->getAll(); //Main query to get users data
          $results = $this->mUsers->getGroupsAndUsers(); //Query for getting the group_id and user_id
        //  Looping both arrays to find matching ID's

          foreach($query as $key=>$value){ //$value['id'] returns the value of the users id from table users
              foreach($results as $row){
                  if ($value['id']==$row['user_id'])
               {
                          array_push($value,"groups",$row['group_id']);

                }

              } 

}

But I doesn’t work. I tried a few other things but nothing by now.

Thanks
Leron

  • 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-01T04:45:14+00:00Added an answer on June 1, 2026 at 4:45 am

    It’s making your life more difficult, and making the data less useful, to try and construct a comma separated string. It makes much more sense to create a sub-array of values, which you can implode(',', $arr) if you need a string representation down the line. It also makes the job a lot easier, and the data more useable, if your array of user data has the same keys as the user_id field:

    // First, get the array of users and format it with sensible keys
    $users = array();
    foreach ($this->mUsers->getAll() as $user) $users[$user['id']] = array_merge($user, array('groups' => array()));
    
    // Now assign group relationships
    foreach ($this->mUsers->getGroupsAndUsers() as $group) {
      $users[$group['user_id']]['groups'][] = $group['group_id'];
    }
    
    print_r($users);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a design of activities like this I have one main activity and
I have this function which is called about 1000 times from main(). When i
I have WPF Application where I have One main form and other user controls
I have a one main Workflow which calls several activities asynchronously. Some of the
i have main activity in which i have Four menus. and i have one
I have one 'main' thread that creates an array of objects of class SlowData
the main tableview will have rows that is added/created from the user and that
I have one spinner in which few values are there from strings.xml and I
This is a weird one. I have a local server on which I develop
I have one main class main, which I use to store the configuration and

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.