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.

6
  • In other words, remove duplicates? Commented Oct 23, 2021 at 8:52
  • Not really, duplicate only for one key : UID (text is not the same. Am I clear ? Commented Oct 23, 2021 at 8:56
  • why sometimes text and other timestamps? what id and text are you putting in the results array? Commented Oct 23, 2021 at 8: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 Commented Oct 23, 2021 at 9: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 Commented Oct 23, 2021 at 9:02

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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"
  }
}

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.