Get all friend_user_id values from wp_bp_friends WHERE initiator_user_id IN ($array_of_ids)

I am making looped queries on my wp_bp_friends database table and returning an array of single-property objects each time. I would like to merge all of these values into a single flat array then implode with commas.

My current code:

Read More
foreach ( $friendsid as $row )
{
    $sql1= "SELECT friend_user_id
            FROM wp_bp_friends
            WHERE initiator_user_id='".$row->friend_user_id."'
              AND is_confirmed = 1";
    $ffid = $wpdb->get_results($sql1);
    // $recur_multi_dimen_arr_obj =  new RecursiveArrayIterator($ffid);
    // $recur_flat_arr_obj =  new     RecursiveIteratorIterator($recur_multi_dimen_arr_obj);
    // $flat_arr = iterator_to_array($recur_flat_arr_obj, false);
    // print_r($flat_arr);
    print_r($ffid);
}

It outputs the following

Array(
    [0] => stdClassObject([friend_user_id] => 62)
    [1] => stdClassObject([friend_user_id] => 51)
    [2] => stdClassObject([friend_user_id] => 60)
    [3] => stdClassObject([friend_user_id] => 65)
    [4] => stdClassObject([friend_user_id] => 56)
)
Array(
    [0] => stdClassObject([friend_user_id] => 43)
    [1] => stdClassObject([friend_user_id] => 50)
    [2] => stdClassObject([friend_user_id] => 64)
    [3] => stdClassObject([friend_user_id] => 45)
    [4] => stdClassObject([friend_user_id] => 44)
)
Array(
    [0] => stdClassObject([friend_user_id] => 57)
)

Using

$resultarray = array();
foreach($ffid as $oneitem){
    $resultarray[]=$oneitem->friend_user_id;
} 
$excluded_user =implode(", ",$resultarray);

I get:

$excluded_user = 57;

What I need to end up with is

$excluded_user = '62, 51, 60, 65, 56, 43, 50, 64, 45, 44, 57;

How can I combine the arrays in $ffid before I use the foreach loop?

Note that the number of arrays in $ffid can vary.

Related posts

Leave a Reply

5 comments

  1. The functions array_map and array_walk_recursive do the trick. A standalone code sample follows:

    function flatten ( $item, $key, &$trg ) {
        array_push($trg, $item);
    }
    function hoist ( $item ) {
        return $item->friend_user_id;
    }
    
    $basearray = array(
                      array(
                          (Object)array("friend_user_id" => 62)
                        , (Object)array("friend_user_id" => 9)
                        , (Object)array("friend_user_id" => 42)
                      )
                    , array(
                          (Object)array("friend_user_id" => 1)
                        , (Object)array("friend_user_id" => 2)
                        , (Object)array("friend_user_id" => 3)
                      )
                    , array(
                          (Object)array("friend_user_id" => 99)
                        , (Object)array("friend_user_id" => 9)
                      )
                 );
    $resultarray = array();             
    array_walk_recursive ( $basearray, 'flatten', &$resultarray );
    print_r($resultarray);
    $resultarray = array_map ( 'hoist', $resultarray );
    print_r($resultarray);
    $excluded_user = implode(", ",$resultarray);
    print_r("excluded_user = " . $excluded_user . "n");
    
  2. You could use array_merge function, to create an array which includes all the data.

    Something like this:

    $arrFfid = array();
    foreach ( $friendsid as $row )
    {
        $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE      initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
        $ffid = $wpdb->get_results($sql1);
    
        $arrFfid = array_merge($arrFfid, $ffid);
    }
    
  3. array_map and array_merge seems to be a good choice for this

    $result = array();
    foreach ( $friendsid as $row ) {
        ...
        ...
        $r = array_map(function($item) {
            return $item->friend_user_id;
        }, $ffid);
        $result = array_merge($result,$r);
    }
    
    $excluded_user = implode(", ", array_unique($result));
    
  4. Use this code:
    Some thing like this

    $resultarray =array();
    foreach ( $friendsid as $row )
    {
       $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
       $ffid = $wpdb->get_results($sql1);
       foreach($ffid as $oneitem){
          $resultarray[] =$oneitem->friend_user_id;
       } 
    }
    $excluded_user =implode(", ",$resultarray);
    
  5. First and foremost, you should not be making multiple trips to the database for this payload of data. You should be passing your array of friend ids to a prepared statement which contains a dynamically populated string of delimited question marks (e.g. initiator_user_id IN (?,?,?)). I even have a demonstration of how to do this with WordPress helpers.

    I don’t use WordPress, but I assume get_col() is the ideal helper method to isolate the results.

    $friendIds = array_column($friendsid , 'friend_user_id');
    $placeholders = implode(',', array_fill(0, count($friendIds), '%s'));
    $sql = "SELECT friend_user_id
            FROM wp_bp_friends
            WHERE is_confirmed = 1
              AND initiator_user_id IN ($placeholders)";
    echo implode(',', $wpdb->get_col($wpdb->prepare($sql, $friendIds)));