3

I have following array, want to merge it and implode last element by comma. First two elements will always be the same,only the last elements will vary, want to implode it by comma.

Input Array

[dob] => Array
    (
        [0] => Array
            (
                [dob] => 20 Feb 1989
                [age] => 28
                [data] => abc
            )

        [1] => Array
            (
                [dob] => 20 Feb, 1989
                [age] => 28
                [data] => xyz
            )

    )

Expected output:

Want to get the following output:-

[dob]  => Array
        (
            [dob] => 20 Feb 1989
            [age] => 28
            [data] => abc,xyz
        )
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Yabes Nadar
  • 209
  • 2
  • 11

1 Answers1

2

You can do it like below:-

$final_array = [];

$final_array['dob'] = [$array['dob'][0]['dob'],$array['dob'][0]['age'],implode(',',array_column($array['dob'],'data'))];

print_r($final_array);

Output:- https://eval.in/887194

OR if you don't want to create a new array then do like below:-

https://eval.in/887204

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98