1

I have an array in php,

array(7) {
  [0]=>
  array(3) {
    ["idu"]=>
    int(4830)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(4)
  }
  [1]=>
  array(3) {
    ["idu"]=>
    int(4831)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(4)
  }
  [2]=>
  array(3) {
    ["idu"]=>
    int(4831)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(5)
  }
  [3]=>
  array(3) {
    ["idu"]=>
    int(4920)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(4)
  }
  [4]=>
  array(3) {
    ["idu"]=>
    int(4920)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(5)
  }
  [5]=>
  array(3) {
    ["idu"]=>
    int(4919)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(4)
  }
  [6]=>
  array(3) {
    ["idu"]=>
    int(4918)
    ["day"]=>
    string(19) "2023-10-21 16:00:00"
    ["type"]=>
    int(4)
  }

}

parameters like 'idu' and 'day' can be duplicated, but 'idu', 'day', 'type' cannot.

How to make the 'type' parameter combined for identical values for 'idu' and 'day'

e.g. for 'idu'=4831 and 'day'='2023-10-21 16:00:00' the 'type' parameter has values 4 and 5,

I would like these two items to become one in the form

...
array(3) {
     ["idu"]=>
     int(4830)
     ["day"]=>
     string(19) "2023-10-21 16:00:00"
     ["type"]=>
     array(2) {
        [0]=>int(4)
        [1]=>int(5)
     }
   }
...
6
  • Can you please put the array in your question using var_export(). It will make the live of the person trying to answer your question a lot easier. Or, to put it another way: You say: "I have an array in php", my response would be: "No you don't, that isn't valid PHP.". Commented Oct 14, 2023 at 22:30
  • Also: If you want to combine two items with 'idu'=4831 why does the resulting item have 'idu'=4830? Commented Oct 15, 2023 at 8:06
  • @volkerschulz Certainly just a typo. Commented Oct 15, 2023 at 8:24
  • You could do something similar to this. Commented Oct 15, 2023 at 8:25
  • @Olivier You're probably right with the typo... Commented Oct 15, 2023 at 8:30

1 Answer 1

1

Here's a quick solution including a test case:

<?php 

function combineDuplicates(Array $original_array) : Array {
    $resulting_array = [];
    foreach($original_array as $item) {
        $hash = bin2hex($item['idu'] . $item['day']);
        if(!isset($resulting_array[$hash])) {
            $resulting_array[$hash] = $item;
            $resulting_array[$hash]['type'] = [$item['type']];
        } else {
            $resulting_array[$hash]['type'][] = $item['type'];
        }
    }
    return array_values($resulting_array);
}

$original_array = [
0=>
[
    "idu"=>
        4830,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        4
],
1=>
[
    "idu"=>
        4831,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        4
],
2=>
[
    "idu"=>
        4831,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        5
],
3=>
[
    "idu"=>
        4920,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        4
],
4=>
[
    "idu"=>
        4920,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        5
],
5=>
[
    "idu"=>
        4919,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        4,
],
6=>
[
    "idu"=>
        4918,
    "day"=>
        "2023-10-21 16:00:00",
    "type"=>
        4
]
];

var_dump($original_array);
var_dump(combineDuplicates($original_array));
Sign up to request clarification or add additional context in comments.

10 Comments

No need to use a hash here. $item['idu'] . ';' . $item['day'] is enough.
By definition, $item['idu'] . ';' . $item['day'] would be a hash too, but you're right, there's no need for md5 here.
You can even use a 2-dimensional array like $result[ idu_value ][ day_value ][] = ... by which you'd even avoid to build a hash at all
If $item['idu'] contains an int and $item['day'] a date, then $item['idu'] . ';' . $item['day'] is not a hash, it's a unique key (no collision possible). Not to mention that it's faster to compute than an MD5.
@HonkderHase This has implications if you want it to return with array_values() however.
|

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.