php - foreach loop and one key to multiple values from sql -


i have problem make 1 array key ( class_id ) , in key lot of users_ids. have function $ids simple array numbers. i'm trying have sth this:

array =>     1 (class_id) =>      0=> 'user_id'      1=> 'user_id',    2 (class_id) =>      0=> 'user_id'      1=> 'user_id' 

now i'm returning ( 1 user_id there should more:

array =>     1=> 'user_id'    2=> 'user_id'  static function getusersidsbyclassids($ids) {     $userids = [];      foreach($ids $classid) {          $object = self::select('user_id')             ->where('class_id', $classid)             ->get();          foreach($object $sth){             $userids[$classid]=$sth->user_id;         }     }      return $userids; } 

i cant fix structure want.

in here

foreach($object $sth){    $userids[$classid] = $sth->user_id; } 

you overwrite value on each iteration. want add new entry:

foreach($object $sth){                 // here    $userids[$classid][] = $sth->user_id; } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -