Home > Net >  PHP array merge elements with same key
PHP array merge elements with same key

Time:10-24

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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!

  • Related