使用array_unique
,它返回没有重复值的新数组。
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
输出
Array
(
[a] => green
[0] => red
[1] => blue
)
Check it out here.
但是,你需要移动$list_array
您for
循环之外,并使用你的条件语句等,使得阵列,
$c=count($_SESSION['cart']);
// if this is in the loop, it will get overwritten
$list_array = array();
for($i=0;$i<$c;$i++){
$id=$_SESSION['list'][$i]['id'];
$person=get_person($id);
// originally, you had $users_array in in_array and array_push
if(!in_array($person, $list_array))
$list_array[] = $person;
}
也指这样的:[PHP :: array_unique为内部阵列阵列(http://stackoverflow.com/questions/5211900/php-array-unique-for-arrays-inside - 阵列) – Panagiotis 2012-03-23 15:10:37
我试图使用此之前,但仍然获得重复的值 – darius 2012-03-23 15:13:16
如果我添加[0]我只得到名字的第一个字母 – darius 2012-03-23 15:21:40