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
Post a Comment