0

This is not a duplicate of the following questions :

I need to merge items in a PHP array, here is an example where I need to merge items with the same UID:

  0 => 
    array (size=10)
      'id' => '958'
      'uid' => '385'
      'text' =>  '2021-10-23 08:32:35'
  1 => 
    array (size=10)
      'id' =>  '956'
      'uid' => '385'
      'text' => '2021-10-23 08:31:51'

  2 => 
    array (size=10)
      'id' => '957'
      'uid' => '386' 
      'timestamp' => '2021-10-23 08:32:12' 

  3 => 
    array (size=10)
      'id' => '955'
      'uid' => '385' 
      'timestamp' => '2021-10-23 08:31:38'

Elements 0, 1 and 3 have the same UID, so the expected array after merging elements with same UID is:

  0 => 
    array (size=10)
      'id' => '958'
      'uid' => '385'
      'text' =>  '2021-10-23 08:32:35'

  1 => 
    array (size=10)
      'id' => '957'
      'uid' => '386' 
      'timestamp' => '2021-10-23 08:32:12' 

I need to keep the item with the smallest key.

Fifi
  • 3,360
  • 2
  • 27
  • 53
  • In other words, remove duplicates? – nice_dev Oct 23 '21 at 08:52
  • Not really, duplicate only for one key : UID (text is not the same. Am I clear ? – Fifi Oct 23 '21 at 08:56
  • why sometimes text and other timestamps? what id and text are you putting in the results array? – splash58 Oct 23 '21 at 08:59
  • I guess, when you say the word, "merge" it comes to picture that you want to work with two arrays. but according to problem it seems like removing duplicates. and on the bottom it says you need only smallest items. all three are three different aspects – Atik Hashmee Oct 23 '21 at 09:01
  • if you want to keep only smallest items in the array. then just sort it and start keeping from the beginning. and if you want to remove the same UUID value then you can remove the duplicates – Atik Hashmee Oct 23 '21 at 09:02
  • @Fifi Regardless, it is still the same notion. See here for [`demo`](http://sandbox.onlinephpfunctions.com/code/8dd8a3e71ec786e8a3a37709012045a48ed20c39) – nice_dev Oct 23 '21 at 09:04

3 Answers3

0

I guess, in case of uid duplicated, you want to keep the first in the array.

This solution might work for you

$no_uid_duplicates = array();
foreach( $array as $key => $item ){
    if(isset($no_uid_duplicates[$item["uid"]])){
        array_splice($array, $key, 1);
    }
    $no_uid_duplicates[$item["uid"]] = 1;
}
C. Celora
  • 429
  • 1
  • 3
  • 12
0

There are number of different ways you can do this. This is only one.

$input[]=array('id'=>'958','uid'=>'385','text'=>'2021-10-23 08:32:35');
$input[]=array('id'=>'956','uid'=>'385','text'=>'2021-10-23 08:31:51');
$input[]=array('id'=>'957','uid'=>'386','timestamp'=>'2021-10-23 08:32:12');
$input[]=array('id'=>'955','uid'=>'385','timestamp'=>'2021-10-23 08:31:38');

$result;
foreach($input as $k=>$v){
    if (! isset($result[$v['uid']])){
        $result[$v['uid']] = $v;
    }
}

// if you do not care about the key
echo '<pre>';
var_dump($result);

// if you want the keys reset
$result = array_values($result);
var_dump($result);

The results, first with uid becoming the array key.

array(2) {
  [385]=>
  array(3) {
    ["id"]=> string(3) "958"
    ["uid"]=> string(3) "385"
    ["text"]=> string(19) "2021-10-23 08:32:35"
  }
  [386]=>
  array(3) {
    ["id"]=> string(3) "957"
    ["uid"]=> string(3) "386"
    ["timestamp"]=> string(19) "2021-10-23 08:32:12"
  }
}

And with the array key re-set.

array(2) {
  [0]=>
  array(3) {
    ["id"]=> string(3) "958"
    ["uid"]=> string(3) "385"
    ["text"]=> string(19) "2021-10-23 08:32:35"
  }
  [1]=>
  array(3) {
    ["id"]=> string(3) "957"
    ["uid"]=> string(3) "386"
    ["timestamp"]=> string(19) "2021-10-23 08:32:12"
  }
}
Tigger
  • 8,980
  • 5
  • 36
  • 40
0

As it was already said, a lot of solutions to solve this problem.

I think the function array_reduce is appropriate for this case.

Here is how you can simply use it (let's suppose your array is named "$arr" here):

// 1. Sort your array in desc order
krsort($arr);

// 2. Reduce your array
$reducedArr = array_reduce($arr, function($reducedArr, $arrValues) {
    $reducedArr[$arrValues['uid']] = $arrValues; // Here we put your 'uid' as a key
    return $reducedArr;
});

// 3. Check your reduced array
print_r($reducedArr);

Hope this helped!

Lagaart
  • 310
  • 1
  • 8