How to make it so that only unique values ​​remain

<?php header('Content-Type: application/json'); function super_unique($array,$key){ $temp_array = []; foreach ($array as &$v) { if (!isset($temp_array[$v[$key]])) $temp_array[$v[$key]] =& $v; } $array = array_values($temp_array); return $array; } $arr = array(); $arr[0]['id'] = null; $arr[0]['name'] = 'John'; $arr[1]['id'] = 12; $arr[1]['name'] = 'John'; $arr[2]['id'] = null; $arr[2]['name'] = null; $arr[3]['id'] = 54; $arr[3]['name'] = 'Ammie'; $arr[4]['id'] = 23; $arr[4]['name'] = 'Martin'; $arr[5]['id'] = 54; $arr[5]['name'] = null; $arr[6]['id'] = 342; $arr[6]['name'] = 'Anna'; $arr[7]['id'] = 64; $arr[7]['name'] = 'Tom'; $arr[8]['id'] = 64; $arr[8]['name'] = null; $arr[9]['id'] = 364; $arr[9]['name'] = null; $arr[10]['id'] = null; $arr[10]['name'] = 'Piter'; $arr = super_unique($arr,'id'); $arr = super_unique($arr,'name'); $arr = array_values($arr); echo json_encode($arr); ?> 

I want to achieve such a result

 [ { "id": 12, "name": "John" }, { "id": 54, "name": "Ammie" }, { "id": 23, "name": "Martin" }, { "id": 342, "name": "Anna" }, { "id": 64, "name": "Tom" }, { "id": 364, "name": null }, { "id": null, "name": 'Piter' } ] 
  • What if the name is the same and the id is different? What element then to take: with the greatest id? - Alexxosipov
  • there will be no such situations - Bagdaulet Sayin
  • You already have this in the code. The name "John" in the array is twice. - Alexxosipov
  • And yes, if such a situation cannot be, then the logic in the question is absent, since you say that it is necessary that only unique values ​​remain. - Alexxosipov
  • there will not be such that for example John 545 and John 748 there can be only null or 1 unique id names are also unique and there is not even a name, but an identifier wrote the name because it is easier to explain - Bagdaulet Sayyn

2 answers 2

Possible through recursion

 function super_unique($array,$temp=[],$key=0){ if ($key==(count($array)-1)) return $temp; if(!array_search($array[$key]['name'],array_column($temp,'name')))$temp[]=[$array[$key]['id']=>$array[$key]['name']]; $key++; return super_unique($array,$temp,$key); else { $key++; return super_unique($array,$temp,$key); } $temp_arr=super_unique($array); 
  • What are key and temp for? - Bagdaulet Sayin
  • For a start, it would be nice to put a tick if it works ... - Artur Han
  • key here is the order key of your arr, and temp is a temporary array to which elements are added - Artur Han
  • I was not able to use this function in php, so I decided to clarify what temp and key echo count (super_unique ($ arrayWithDuplicates)) is responsible for; - Bagdaulet Sayin
  • You do not need them in the arguments, they are used only within the function itself, and in the argument specify only the source array itself - Artur Han

In stackoverflow in English, I was given the following answer:

 //This function will get you the missing field from the array: function getField($arr, $key, $value, $newKey) { foreach($arr as $e) if (($e[$key] == $value) && $e[$newKey]) return $e[$newKey]; } function uniqueArray($arr){ $res = array(); foreach($arr as $a) { if (!$a["id"] && !$a["name"]) continue; // if both null ignore if (!$a["id"]) // if id missing go get it $a["id"] = getField($arr, "name", $a["name"], "id"); if (!$a["name"]) // fill the name if missing $a["name"] = getField($arr, "id", $a["id"], "name"); $res[] = $a; // as to result array } $r = array_map("json_decode", array_unique(array_map("json_encode", $res))); return array_values($r); }