我想从我的数组中删除重复的条目,我的数组是
ArrayTotal 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,
我们要删除重复的条目我正在尝试php unique_array并且我也试过javascript
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
console.log(uniqueNames) its gave error Unexpected token A
答案 0 :(得分:0)
尝试使用php,
$dup=array();
foreach($bname as $k=>$v) {
if( ($kt=array_search($v,$bname))!==false and $k!=$kt )
{ unset($unique[$kt]); $dup[]=$v; }
}
答案 1 :(得分:0)
你应该可以在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。
答案 2 :(得分:0)
尝试这样Demo Here
var names = ["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"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
console.log(uniqueNames);