1. 程式人生 > >PHP計算陣列差集

PHP計算陣列差集

手冊中本來有array_diff,但是效率可以改進

  

function arrayDiffEmulation($arrayFrom, $arrayAgainst)
{
$arrayAgainst = array_flip($arrayAgainst);

foreach ($arrayFrom as $key => $value) {
if(isset($arrayAgainst[$value])) {
unset($arrayFrom[$key]);
}
}

return $arrayFrom;
}