2015-03-25 74 views
1

我要删除我的数组重复条目数组重复值,我的数组是删除从阵列中的JavaScript

 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

unique_array应该这样做,你可以发表你的PHP代码 – 2015-03-25 06:45:55

+0

什么问题? array_unique没有为你工作?把你的代码放在正确的格式,以便我们可以帮你 – 2015-03-25 06:46:44

+0

@preeti array_unique()不适用于多维数组。 – 2015-03-25 06:50:18

回答

0

尝试一下本作的PHP,

$dup=array(); 
foreach($bname as $k=>$v) { 

if(($kt=array_search($v,$bname))!==false and $k!=$kt) 
{ unset($unique[$kt]); $dup[]=$v; } 

} 
+0

我曾尝试过,但没有工作 – Preeti 2015-03-25 07:05:12

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

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); 
+0

我的数组是这样的问题那ArrayTotal所有银行,所有银行总数,国内银行总额,国内银行总额,B2B银行,B2B银行,蒙特利尔银行,蒙特利尔银行,新斯科舍银行,新斯科舍银行 – Preeti 2015-03-25 07:08:27

+0

你能解释清楚不能得到您 – 2015-03-25 07:09:35