0

I have an simple array like this.

array:4[
0 => array:3 [
'customerName' => "John Dunk"
'customerPhone' => 5555555555
'Difference' => 125.0
]
1=> array:3 [
'customerName' => "John Dunk"
'customerPhone' => 5555555555
'Difference' => 192.0
]
2=> array:3 [
'customerName' => "James Morle"
'customerPhone' => 111111111
'Difference' => 145.0
]
3=> array:3 [
'customerName' => "James Morle"
'customerPhone' => 111111111
'Difference' => 114.0
]
]

But I want to make that array like this one.

array:2[
0 => array:3 [
'customerName' => "John Dunk"
'customerPhone' => 5555555555
'Difference' => 125.0
]
1=> array:3 [
'customerName' => "James Morle"
'customerPhone' => 111111111
'Difference' => 114.0
]
]

So as you understand:

If inside of multidimensional array 2 or more array has SAME "customerName", choose the smallest "Difference" one and delete the other arrays.

If you can't understand you can look my example.

Everytime the smallest "Difference" index is going to stay, if 2 or more array has SAME "customerName" We are going to look that arrays and search for the smallest, when we found that array, we will delete the other ones.

I couldn't code this for hours. If anybody can help, I will be very happy realy.

Thanks

  • please add your try and select a single language. – Nina Scholz Aug 14 '20 at 17:13
  • Okay, I added only php. But what do you mean "add your try"? I added my result. I want the result like the second one. – Emrecan Ozkan Aug 14 '20 at 17:15
  • By "add your try", they mean for you to post the code that you tried, even if it wasn't successful, which at least gives us something to work with – Chris Haas Aug 14 '20 at 17:17
  • Okay, I understood but the problem is here. I don't have any code block to show you. I only have that first multidimensional array and want to make the second multidimensional array. I want to know how to make it, want to know the algorithm. – Emrecan Ozkan Aug 14 '20 at 17:23

1 Answers1

0

One way is to sort descending on Difference and then extract and re-index on customerName. This will extract the last ones of customerName which are the lowest Difference:

array_multisort(array_column($array, 'Difference'), SORT_DESC, $array);
$result = array_column($array, null, 'customerName');

If needed to re-index with integers:

$result = array_values($result);

If you wanted the highest Difference then SORT_ASC.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87