带有array_merge和array_diff的PHP数组

时间:2015-05-20 12:00:52

标签: php arrays

我有以下代码

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

$resultarray =array();
foreach($result as $oneitem){
    $resultarray[]=$oneitem->friend_user_id;
} 
$results = implode(", ",$result);
print_r($resultarray);
echo $results;


$excluded_user = array_diff($usersarray , $resultarray);
$excluded_user =implode(", ",$excluded_user); //comma separated ids of     users whom you want to exclude
echo $excluded_user;

这为$ usersarray输出以下内容:

Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 42 [6] => 43 [7] => 44 [8] => 45 [9] => 46 [10] => 47 [11] => 48 [12] => 49 [13] => 50 [14] => 51 [15] => 52 [16] => 53 [17] => 54 [18] => 55 [19] => 56 [20] => 57 [21] => 58 [22] => 59 [23] => 60 [24] => 61 [25] => 62 [26] => 63 [27] => 64 [28] => 65 [29] => 66 [30] => 86 [31] => 103 [32] => 121 [33] => 123 [34] => 124 [35] => 125 [36] => 143 [37] => 147 [38] => 149 [39] => 150 )

但是对于$ resultarray我得到了这个:

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )

然而,当我回显$ results时,我得到:64,56,53,47,44,57,43,50,1,47,59这是正确的ID列表。

似乎是在$ excluded_user = array_diff($ usersarray,$ resultarray)中分解;阶段。我怀疑这是因为$ resultarray的格式导致array_diff()无法正常工作。

任何人都可以建议为什么$ resultarray会得到这样的输出?或者,如果这甚至是代码掉落的地方。

提前感谢任何建议,非常感谢。

0 个答案:

没有答案