There is an array

Array ( [0] => Array ( [ID] => 0 [NAME] => Все типы ресторанов ) [1] => Array ( [ID] => 126 [NAME] => Бизнес ланчи ) [2] => Array ( [ID] => 128 [NAME] => Кафе ) [3] => Array ( [ID] => 130 [NAME] => Кейтеринг ) [4] => Array ( [ID] => 132 [NAME] => Кондитерская ) [5] => Array ( [ID] => 133 [NAME] => Пироговая ) [6] => Array ( [ID] => 134 [NAME] => Пиццерия ) [7] => Array ( [ID] => 27 [NAME] => Ресторан ) [8] => Array ( [ID] => 78 [NAME] => Ресторан ) [9] => Array ( [ID] => 135 [NAME] => Ресторан ) [10] => Array ( [ID] => 26 [NAME] => Суши ) [11] => Array ( [ID] => 138 [NAME] => Суши ) ) 

Remove duplicates using array_unique failed. How to be?

  • Please write an example, how to remove duplicate arrays by key? - Ademard
  • If you have a new question, please ask it by clicking the " Ask a Question " button. Give a link to this question if it provides the necessary context. - BOPOH

2 answers 2

That's right. array_unique() removes duplicate values. And in this array, the value of the element is also an array of two elements. First you need to make a new array with keys from ID and values ​​from NAME , and then use array_unique() .

UPD: And yet, something tells me that this is the result of sampling from the database. If so, you can select unique values ​​using an SQL query.

  • Sample query: SELECT DISTINCT (TYPE_ID) AS ID, TYPE AS NAME FROM table WHERE CITY_ID = '2' ORDER BY TYPE ASC - Tchort
  • Such a query will select all unique names and the very first identifiers corresponding to these names: SELECT MIN (TYPE_ID) AS ID, TYPE AS NAME FROM it_catalog_index WHERE CITY_ID = '2' GROUP BY TYPE ORDER BY TYPE ASC - KiTE
 public String[] massUnique (String [] strings){ // returns String array with unique values Set<String> set = new HashSet<>(); for (String s: strings){ set.add(s); } String result[]; Iterator <String> iterator = set.iterator(); String [] resMassiv = new String[set.size()]; int index = 0; while (iterator.hasNext()){ resMassiv [index] = iterator.next(); index++; } return resMassiv; } 

The method will remove duplicates from the Strig array.

  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky