简体   繁体   中英

How to compare two arrays and remove data from one array in php?

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?

use

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

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

是的,array_diff可能有帮助,请访问http://php.net/manual/en/function.array-diff.php

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);
$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM