0

I have two arrays that contain friend objects. I want to find the IDs that match each other to implement mutual friends functionality. If it's too complicated using PHP, maybe I'll do it in the mysql way.

These are example array items:

$array1 = array(1) {
["friends_list"]=>
array(3) {
[0]=>
object(stdClass)#29 (8) {
  ["notifica_ID"]=>
  string(4) "2673"
}
[1]=>
object(stdClass)#30 (8) {
  ["notifica_ID"]=>
  string(4) "3532"
}
[2]=>
object(stdClass)#31 (8) {
  ["notifica_ID"]=>
  string(4) "3649"
}
}
}

$array2 = array(1) {
["friends_list"]=>
array(5) {
[0]=>
object(stdClass)#32 (8) {
  ["notifica_ID"]=>
  string(4) "1679"
}
[1]=>
object(stdClass)#33 (8) {
  ["notifica_ID"]=>
  string(4) "2137"
}
[2]=>
object(stdClass)#35 (8) {
  ["notifica_ID"]=>
  string(4) "3186"
}
[3]=>
object(stdClass)#36 (8) {
  ["notifica_ID"]=>
  string(4) "3187"
}
[4]=>
object(stdClass)#37 (8) {
  ["notifica_ID"]=>
  string(4) "3649"
}
}
}

I want to the result to be:

$result = array(1) {
["friends_list"]=>
array(1) {
[0]=>
array(
  ["notifica_ID"]=>
  string(4) "3649"
)
}
}

I have already tried array_intersect method but I don't know how can I implement it in array of objects.

2
  • @dave im sorry for my bad sentence. Commented Jun 30, 2015 at 4:05
  • No need to apologise. It's a community aimed at improving the questions and answers. Commented Jun 30, 2015 at 6:09

2 Answers 2

2

Not straightforward solution, But this will work..

$new_array1 =   obj_to_array( $array1["friends_list"] );
$new_array2 =   obj_to_array( $array2["friends_list"] );

$temp_result    =   array_uintersect( $new_array1, $new_array2, 'compare_values' );

$result =   array( "friends_list" => $temp_result );

function obj_to_array( $input1 )
{
    foreach( $input1 as $new )
        $temp_array[]   =   (array)$new;

   return $temp_array;
}
function compare_values($input1, $input2)
{
   return strcmp($input1['notifica_ID'], $input2['notifica_ID']);
}

$result is what you are expecting.

Sign up to request clarification or add additional context in comments.

Comments

0

I decided to put id's in other arrays. then do the array_intersect method. this is now my answer.

public function mutual_friends($my_ID = '' ,$friend_ID = ''){
    if($friend_ID != '' OR $my_ID != ''){
        $myfr = $this->get_friends($my_ID);
        $frofmy = $this->get_friends($friend_ID);            
        $array1 = array();
        $array2 = array();
        foreach($myfr['friends_list'] as $row){
            $array1[] = $row->notifica_ID;
        }
        foreach($frofmy['friends_list'] as $row){
            $array2[] = $row->notifica_ID;
        }
        $result = array_intersect($array1,$array2);
        if(count($result) > 0){
            $mf = array();
            $i = 0;
            foreach($myfr['friends_list'] as $row){
                foreach($result as $row1){
                    if($row1 == $row->notifica_ID){
                        $mf[$i]['notifica_ID'] = $row->notifica_ID;
                        $i++;
                    }
                }
            }
            return $mf;
        }
        else{
            return false;
        }
    }else{
        return false;
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.