0

Im some new in PHP and I want to join values according to drug key with the same id.

My array:

Array
(
    [0] => Array
        (
            [id] => 17
            [drug] => Clobazam
        )

    [1] => Array
        (
            [id] => 23
            [drug] => Dexametasona
        )

    [2] => Array
        (
            [id] => 23
            [drug] => Diiodohidroxiquinoleína
        )

    [3] => Array
        (
            [id] => 25
            [drug] => Diclofenac,dietilamina
        )

Expected output:

Array
(
    [0] => Array
        (
            [id] => 17
            [drug] => Clobazam
        )

    [1] => Array
        (
            [id] => 23
            [drug] => Dexametasona, Diiodohidroxiquinoleína
        )

    [2] => Array
        (
            [id] => 25
            [drug] => Diclofenac,dietilamina
        )
Aksen P
  • 4,564
  • 3
  • 14
  • 27
I. Munoz
  • 35
  • 5
  • Do you get this data from your database? – nice_dev Dec 06 '19 at 13:25
  • No, from a diferent xml files. This array is a that a fusion of several files. – I. Munoz Dec 06 '19 at 13:26
  • Loop over your data, put the items into a new array. While you are doing so, check if an item with the same id value already exists in the new array - if so, you need to take the current `drug` value of that item, combine it into an array with the `drug` from the current record, and assign the whole thing _to_ the `drug` key of the already existing item again. – 04FS Dec 06 '19 at 13:28
  • This should give you a good hint. https://stackoverflow.com/questions/12706359/php-array-group – nice_dev Dec 06 '19 at 13:33

2 Answers2

2

$array = [
    [
        'id' => 17,
        'drug' => 'Clobazam'
    ],
    [
        'id' => 23,
        'drug' => 'Dexametasona'
    ],
    [
        'id' => 23,
        'drug' => 'Diiodohidroxiquinoleína'
    ],
    [
        'id' => 25,
        'drug' => 'Clobazam'
    ]
];

$result = [];

foreach ($array as $item) {
    $id = $item['id'];
    $drug = $item['drug'];
    if (isset($result[$id])) {
        $result[$id]['drug'] .= ', ' . $drug;
    } else {
        $result[$id] = $item;
    }
}

$result = array_values($result);

var_dump($result);
0

You can use this function for to retrieve your expected output:

function collect_same_id(&$data){

        $tmp = [];
        $i = 0;
        foreach($data as $ind=>$rec){

             if(!array_key_exists($rec['id'],$tmp)){ 
                $tmp[$rec['id']] = $i;
                $i++;
            } else {
                $data[$tmp[$rec['id']]]['drug'] .= ', '.$rec['drug'];
                unset($data[$ind]); 
            } 
        }
        sort($data);
}

collect_same_id($data);

print_r($data);

Demo

Outputs:

Array
(
    [0] => Array
        (
            [id] => 17
            [drug] => Clobazam
        )

    [1] => Array
        (
            [id] => 23
            [drug] => Dexametasona, Diiodohidroxiquinoleína
        )

    [2] => Array
        (
            [id] => 25
            [drug] => Diclofenac,dietilamina
        )

)

Here & mutate your original array, you can read about references here. Appending values done with concatenation. Information about how does array_key_exists works you can read here.

Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • I wouldn't recommend this as it is not efficient. Also, sorting the dataset `$data` inside the loop of `$data` itself should be purely avoided. – nice_dev Dec 06 '19 at 13:57
  • @vivek_23, it's not necessary, I know. Just thinking about other stuff – Aksen P Dec 06 '19 at 13:59