2010-01-12 26 views

回答

10

您可以只使用一個數組,並將所需的數據放入密鑰中,因爲密鑰不能重複。

4

SplObjectStorage是最近的事情。

$storage = new SplObjectStorage; 
$obj1 = new StdClass; 

$storage->attach($obj1); 
$storage->attach($obj1); // not attached 
echo $storage->count(); // 1 

$obj2 = new StdClass; // different instance 
$obj3 = clone($obj2); // different instance 

$storage->attach($obj2); 
$storage->attach($obj3);  
echo $storage->count(); // 3 

顧名思義,它只能在對象工作雖然。如果您想要在標量類型中使用此功能,則必須使用新的Spl Types作爲替代,以及使用Spl Data StructuresArrayObject替代陣列。

5

可以使用值的標準PHP數組,並將其傳遞通過array_unique功能:

$input = array(4, "4", "3", 4, 3, "3"); 
$result = array_unique($input); 
var_dump($result); 

輸出:

array(2) { 
    [0] => int(4) 
    [2] => string(1) "3" 
} 
相關問題