你应该能够做到这一点在PHP array_unique,像这样:
// Your existing array
$items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// array_unique does the dirty work for you
$noduplicates = array_unique($items);
// results are in $noduplicates
print_r($noduplicates);
这是在PHP没有array_unique:
// Your existing array
$items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// Our new array for items
$noduplicates = [];
// Loop through all items in an array
foreach($items as $item) {
// Check new array to see if it's there
if(!in_array($item, $noduplicates)) {
// It's not, so add it
$noduplicates[] = $item;
}
}
// results are in $noduplicates
print_r($noduplicates);
这是在Javascript中 - 你不需要使用jQuery这个任务:
// Your existing array
var items = [ "Total All Banks", "Total All Banks", "Total Domestic Banks", "Total Domestic Banks", "B2B Bank", "B2B Bank", "Bank of Montreal", "Bank of Montreal", "The Bank of Nova Scotia", "The Bank of Nova Scotia" ];
// Our new array for items
var noduplicates = [];
// Loop through all items in an array
for (var i = 0; i < items.length; i++) {
// Check new array to see if it's already there
if(noduplicates.indexOf(items[i]) == -1) {
// add to the new array
noduplicates.push(items[i]);
}
}
// results are in noduplicates
console.log(noduplicates);
小提琴是available here。
unique_array应该这样做,你可以发表你的PHP代码 – 2015-03-25 06:45:55
什么问题? array_unique没有为你工作?把你的代码放在正确的格式,以便我们可以帮你 – 2015-03-25 06:46:44
@preeti array_unique()不适用于多维数组。 – 2015-03-25 06:50:18