1

i have 2 arrays of PHP, one having ids of online users and second having ids of idle users, but in idle users array some ids of online user's also exist, i want to compare both arrays and remove user ids from 2nd array in php. how can i do this?

3
  • you mean offline users from idle's array . right? Commented Mar 2, 2015 at 9:55
  • or you want to remove those ids who exist in online array? Commented Mar 2, 2015 at 9:57
  • i want to remove those ids whic exist in online users array. Commented Mar 2, 2015 at 9:57

4 Answers 4

1

use

$idleWithoutOnline = array_diff($idleUsers, $onlineUsers);

http://php.net/manual/en/function.array-diff.php

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

Comments

0

yes thats possible by array_diff have a look at http://php.net/manual/en/function.array-diff.php

Comments

0

Use this

$array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
$array2 = array('value1'   ,'demo' ,'value2'   ,'some' ,'value3');

array_unique( array_merge($arr_1, $arr_2) );

or you can do:

$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);

Comments

0
$online_users = array(1, 5, 7, 8);
$idle_users = array(6, 4, 5, 8, 9);

for($i = 0; $i < count($idle_users); $i++)
{
    foreach($online_users as $v)
    {
        if($idle_users[$i] == $v)
        {
            unset($idle_users[$i]);
        }
    }
}

print_r($idle_users); // This will give you 6,4,9 as idle users

Comments

Your Answer

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