1

I want to remove duplicate entry from my array and my array is

        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,

we want to remove duplicate entry for this I am trying in php unique_array and I also tried in 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
Preeti
  • 11
  • 4

3 Answers3

0

try this for php,

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

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

}
Ayyanar G
  • 1,535
  • 1
  • 11
  • 22
0

You should be able to do it in PHP with array_unique, like so:

// 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);

Here it is in PHP without 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);

Here it is in Javascript - you don't need to use jQuery for this task:

// 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);

Fiddle is available here.

Mark Hughes
  • 398
  • 2
  • 16
0

Try like this 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);
I'm Geeker
  • 4,646
  • 5
  • 20
  • 40
  • my array is like this problem with that: 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 – Preeti Mar 25 '15 at 07:08
  • can u explain clearly cant get you – I'm Geeker Mar 25 '15 at 07:09