-1

Hi I'm trying to remove the results of one array from another and store the result as a comma seperated list of strings.

This is the code I have so far:

function bpdev_include_users(){
 //list of users to exclude

global $wpdb;
$frndsof = array();
$frnds = array();
$result = array();
$friendsid = array();

$currentuser_id = get_current_user_id();
echo $currentuser_id;
?><br />
<?php

$sql= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$currentuser_id."' AND is_confirmed = 1";
$friendsid = $wpdb->get_results($sql);
print_r($friendsid);
?><br />
<?php
$myexcluded_users=array();
foreach($friendsid as $oneitem){
    $myexcluded_users[]=$oneitem->friend_user_id;
} 
$excluded_user =implode(", ",$myexcluded_users);
echo $excluded_user;
?><br />
<?php

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);
print_r($ffid);
?><br />
<?php
}

More code (sorry weird formatting issues...)

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


$sql2= "SELECT user_id FROM wp_bp_xprofile_data";
$users = $wpdb->get_results($sql2);



$result = array_merge($friendsid, $ffid);


//$excluded_user = array_diff($users , $result); //comma separated ids of users whom you want to exclude

}


bpdev_include_users();

What I'm trying to end up with is

$excluded_user = 1, 2, 3, 4;

Currently I'm getting an error at the array_diff stage: Object of class stdClass could not be converted to string

This is the output from the above code

1

Array ( [0] => stdClass Object ( [friend_user_id] => 54 ) [1] => stdClass Object ( [friend_user_id] => 48 ) [2] => stdClass Object ( [friend_user_id] => 61 ) )

54, 48, 61

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

57

Thanks in advance for any help!

1
  • Are you sure it should be $ffid = $wpdb->get_results($sql1) and not $ffid .= $wpdb->get_results($sql1) ? (concatenation) Commented Feb 24, 2015 at 17:35

2 Answers 2

1

You need to access the stdclass in the array.

$myexcluded_users = [];

foreach ($excluded_user as $oneitem) {
    $myexcluded_users[] = $oneitem->friend_user_id;
} 

$excluded_user = implode(', ', $myexcluded_users);

Edit:

So this line,

$excluded_user = array_diff($users , $result); // comma separated ids of users whom you want to exclude

both $users and $result are the arrays that have the stdClass inside them?

How about this?

$usersarray = [];
    
foreach ($users as $oneitem) {
    $usersarray[] = $oneitem->friend_user_id;
} 

$resultarray = [];
    
foreach ($result as $oneitem) {
    $resultarray[] = $oneitem->friend_user_id;
} 

$excluded_user = array_diff($usersarray, $resultarray);
$excluded_user = implode(', ', $excluded_user);
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your suggestion - I think it has definitely put me in the right direction. I've edited the original question now to reflect how I've tried to implement this
I'm confused by your edit. The snippet I posted just needed to be added at the bottom after $excluded_user = array_diff($users , $result); //comma separated ids of users whom you want to exclude It takes the current result that you had in $excluded_user and formats it to the output that you wanted.
I cant include $excluded_user = array_diff($users , $result); //comma separated ids of users whom you want to exclude - that's what gives me the Object of class stdClass could not be converted to string
thanks for your help and patience - I've accepted this as the answer. There is still one part which is confusing me though. When I use print_r($ffid); it outputs 3 arrays and using implode and echo outputs the id 57, ignoring the id's in the other arrays (you can see this output in my question). This messes up the result in $excluded_user... Have you any idea why it might put the $ffid into 3 arrays?
just figured it out $friendsid has 3 results,hence the 3 arrays.... Just need to work out how to store them all in $results now!
|
1

You can use implode() for this purpose. This function will separate the array using the string you specified. Check out this reference Use the code below

echo implode(", ", array("HELLO", "WORLD", "lan", "Butler")); // Will Seprate the array

Hope this helps you

1 Comment

that also gives the error Object of class stdClass could not be converted to string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.