使用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