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