Merge these array values into a new array?

75 views Asked by At

I have the following array stored in $data

Array
(
    [name] => JimBob
    [data] => Array
        (
            [0] => Array
                (
                    [date] => 2013-15-5
                    [pole] => 2
                    [race] => 43
                )

            [1] => Array
                (
                    [date] => 2013-15-6
                    [pole] => 15
                    [race] => 34
                )

            [2] => Array
                (
                    [date] => 2013-15-7
                    [pole] => 9
                    [race] => 54
                )

        )

)

I need to create a new array which looks like this

Array
(
    [name] => JimBob
    [data] => Array
        (
            [0] => 2013-15-5,2
            [1] => 2013-15-6,15
            [2] => 2013-15-7,9
        )

)

So far I have this array_map function

$arr2['data'] = array_map(function($date) {
    return $date['date'];
}, $data['data']);

print_r($arr2);

Which outputs the below, how can I get the pole values added to these dates separated by comma?

Array
(
    [data] => Array
        (
            [0] => 2013-15-5
            [1] => 2013-15-6
            [2] => 2013-15-7
        )

)
1

There are 1 answers

0
Yogesh Suthar On BEST ANSWER

Use concatenation.

$arr2['data'] = array_map(function($date) {
    return $date['date'] . ',' . $date['pole'];
}, $data['data']);