1

I know this question was already been asked before, but none seems to solve my problem.

I have DB where one user can have multiple rows. Only identifier for each is UID/UUID. Nothing else. Other values can be totally different.

I do the query which selects all rows in table. This returns an multidimensional array:

Array
(
    [0] => Array
        (
            [meta] => 213097312903
            [UUID] => 1
            [data_name] => wheel_id
            [data_value] => 13940
        )

    [1] => Array
        (
            [meta] => 2217037902293
            [UUID] => 1
            [data_name] => car_id
            [data_value] => 09278149
        )

    [2] => Array
        (
            [meta] => 201207386198
            [UUID] => 12
            [data_name] => car_id
            [data_value] => 639781238
        )
)

What I would like to accomplish is to process given multidimensional array and merge all values for same UUID like this:

Array
(
    [0] => Array
        (
            [meta] => 213097312903,2217037902293
            [UUID] => 1
            [data_name] => wheel_id,car_id
            [data_value] => 13940,09278149
        )

    [1] => Array
        (
            [meta] => 201207386198
            [UUID] => 12
            [data_name] => car_id
            [data_value] => 639781238
        )
)

It doesn't to be exactly like this, I just don't want data for same user to be all around array.
For further processing, I need all users, so I cannot simply query select all where ID is...
I'm stuck with this. :(

2 Answers 2

1

You can loop that array and create a new one:

foreach($data as $value)
{
    $output[$value['UUID']]['meta'][] = $value['meta'];
    $output[$value['UUID']]['data_name'][] = $value['data_name'];
    $output[$value['UUID']]['data_value'][] = $value['data_value'];
}

if you want to convert the arrays to comma seperated values:

foreach($output as $uuid => $value)
{
    $outputWithCSV[$uuid]['meta'] = implode(",",$value['meta']);
    $outputWithCSV[$uuid]['data_name'] = implode(",",$value['data_name']);
    $outputWithCSV[$uuid]['data_value'] = implode(",",$value['data_value']);
}

$outputWithCSV would be:

Array
(
    [1] => Array
        (
            [meta] => 213097312903,2217037902293
            [data_name] => wheel_id,car_id
            [data_value] => 13940,09278149
        )

    [12] => Array
        (
            [meta] => 201207386198
            [data_name] => car_id
            [data_value] => 639781238
        )
)
1
  • Thanks, worked like charm. From your answer I got another idea how to accomplish the same which better suits me. :) Commented Apr 30, 2015 at 9:29
1

Create a new container which is based from that ID of yours, if its set already, just concatenate the next match:

$data = array();
foreach($array as $values) {
    if(!isset($data[$values['UUID']])) {
        $data[$values['UUID']] = $values;
    } else {
        $data[$values['UUID']]['meta'] .= ', ' . $values['meta'];
        $data[$values['UUID']]['data_name'] .= ', ' . $values['data_name'];
        $data[$values['UUID']]['data_value'] .= ', ' . $values['data_value'];
    }
}

Sample Output

1
  • Thank you, also works like charm, but Volkan Ulukut was first so I accepted his answer. Commented Apr 30, 2015 at 9:33

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.