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
  • unique_array should do it, can you post your php code –  Mar 25 '15 at 06:45
  • What's the problem? array_unique didn't work for you?Put your code in correct format so that we can help you – Alive to die - Anant Mar 25 '15 at 06:46
  • @preeti array_unique() is not work on multi dimensional arrays. – Ayyanar G Mar 25 '15 at 06:50
  • refer this link http://stackoverflow.com/questions/307650/how-to-remove-duplicate-values-from-an-array-in-php – Ayyanar G Mar 25 '15 at 06:51
  • @RobMullins $bname=array(); when I print bname its value are : 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, $bankarray=array(); $bankarray = array_unique($bname); print_r($bname); – Preeti Mar 25 '15 at 06:52
  • @Preeti did you try any of my solutions? – Mark Hughes Mar 25 '15 at 07:11
  • 1
    @AyyanarG `array_unique()` can work with multi dimensional arrays; see also [this answer](http://stackoverflow.com/a/18373723/1338292). – Ja͢ck Mar 25 '15 at 07:19
  • nice to know that...@Ja͢ck – Ayyanar G Mar 25 '15 at 07:20
  • This already has an answer. http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php – web-nomad Mar 25 '15 at 07:21

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,545
  • 1
  • 11
  • 24
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
  • 418
  • 3
  • 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,601
  • 5
  • 22
  • 41
  • 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