0

I have a social site that contains follow users system.

Each user has two custom meta:

  • following_users
  • followers_users

My question how can get the users that my following users are following?

I try something like this but it's not working:

$get_the_following_users = get_user_meta($current_user->ID, 'following_users', true);
foreach ( $get_the_following_users as $following_user ) {
    $following_user = get_user_meta($following_user, 'following_users', true);

    $meta_qssuery .= $following_user.', ';
}

echo $meta_qssuery;

Note: I store the array meta of following_users and followers_users like this:

a:8:{i:9;i:9;i:57;i:57;i:3;i:3;i:71;i:71;i:85;i:85;i:89;i:89;i:152;i:152;i:2;i:2;}
2
  • you're adding the ids of the current user's followers to your output, and not the ids of the followers of the current user's followers.
    – inarilo
    Commented Dec 26, 2017 at 6:25
  • @inarilo sorry it's a mistake I edited now the question and try again it's print to me Array, Array, Array, Array, (i think I must use another foreach but I cant do this i need a help).
    – Michel
    Commented Dec 26, 2017 at 6:35

1 Answer 1

1

You can use the implode function to concatenate array elements:

$get_the_following_users = get_user_meta($current_user->ID, 'following_users', true);
$meta_qssuery = ''; //initialise to an empty string
foreach ( $get_the_following_users as $following_user ) {
    $following_user = get_user_meta($following_user, 'following_users', true);

    if(!empty($following_user)) { //check if any data was returned
        $meta_qssuery .= implode(', ', $following_user).', ';
    }
}

echo $meta_qssuery;

This will leave an extra ', ' at the end of the string. This can be removed after the loop ends, or you can modify the loop:

$get_the_following_users = get_user_meta($current_user->ID, 'following_users', true);
$get_the_following_users_2 = array();
foreach ( $get_the_following_users as $following_user ) {
    $following_user = get_user_meta($following_user, 'following_users', true);

    if(!empty($following_user)) { //check if any data was returned
        $get_the_following_users_2[] = implode(', ', $following_user);
    }
}

$meta_qssuery = implode(', ', $get_the_following_users_2);
echo $meta_qssuery;
3
  • Thank you, but it's getting the users that I follow not the users that my following users follow to solve this change the variable name of $get_the_following_users from $meta_qssuery and $get_the_following_users[$i]
    – Michel
    Commented Dec 26, 2017 at 13:00
  • another thing if there are no results you will get this error: Warning: implode(): Invalid arguments passed in this line ($meta_qssuery = implode(', ', $get_the_following_users);). please solve this.
    – Michel
    Commented Dec 26, 2017 at 13:11
  • i did not consider a scenario where there are no results, in which case the original array $get_the_following_users remained unchanged. I have modified the code to use a separate array variable instead,.
    – inarilo
    Commented Dec 26, 2017 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.