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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.