Yii2 use a Foreach loop to iterate through a user group to retrieve an ID to another table to search for information and bring information to merge meta-array information---case
Public Function actionrandomlists () {
Information is queried for all users
$UserInfo =useroperate::find ()->select (' Id,username,sex,signature,lng,lat,imgs ')->asarray ()->all ();
Loop through the ID you want in all of your user information, and then go to another data table to find what you want.
foreach ($UserInfo as $key = = $val) {
$user _id = Explode (', ', $val [' id ']);
$PetList []=petoperate::find ()->select (' Pet_sex,pet_category,pet_name,pet_imgs ')->where ([' user_id ' =>$ USER_ID])->asarray ()->all ();
(supports multiple queries) the second time iterates through the information of another data table based on the ID you get
$UserFriendInfo =userfriendoperate::find ()->where ([' user_id ' = $user _id])->asarray ()->all ();
foreach ($UserFriendInfo as $k = = $v) {
Compare to see if there are any arrays in the array to be compared.
$array [] = $v [' friend_id '];
if (In_array ($user _id, $array)) {
If so, the way to return is as follows
$UserInfo [$key] [' is_friend '] = ' true ';
}else{
If not then return the following way
$UserInfo [$key] [' is_friend '] = ' false ';
}
}
}
Array merging
foreach ($UserInfo as $key = = $val) {
$UserInfo [$key] [' pet '] = $PetList [$key];
}
After the interface logic processing is complete, return the data according to the type of the return value encapsulated by the framework
Return Helper::format_data (SUCC, $UserInfo);
}
Yii2 Remove ID from one user group through a foreach loop to search for information in another table and merge the original array information with the information---case