0

I have this type array

Array
(
    [0] => Array
        (
            [id] => 90
            [name] => Paul
            [company] => Google
            [date] => 2018-01-06
            [total] => 100.00
        )
    [1] => Array
        (
            [id] => 90
            [name] => Paul
            [company] => Google
            [date] => 2018-07-06
            [total] => 100.00
        )
    [2] => Array
        (
            [id] => 89
            [name] => Ethan
            [company] => Yahoo
            [date] => 2018-07-10
            [total] => 1140.00
        )
)

I need to create a new array from the previous one by merging id and name if they are the same.

The desired output should be:

[[
    [id] => 90
    [name] => Paul
    [company] => Google,
    [data] => [
        [date] => 2018-01-06,
        [total] => 100.00
    ],
    [
        [date] => 2018-07-06,
        [total] => 100.00
    ],
], [
    [id] => 89
    [name] => Ethan
    [company] => Yahoo
    [data] => [
        [date] => 2018-07-10,
        [total] => 1140.00
    ]
]]

What I have tried before:

$output = array();
foreach($input as $data){
    $output[$data['id']][] = $data;
    $output[$data['name']][] = $data;
    $output[$data['company']][] = $data;
}

What I'm missing here please ?

Thanks.

7
  • so, you need no duplicates or just checking 2 consecutive elements if they are same is enough? Commented Jul 11, 2018 at 12:47
  • Also, if you're getting the data from the database, I have the feeling you can solve this problem by modifying your query instead of php code. It may be more efficient Commented Jul 11, 2018 at 12:48
  • @Lemures, I need to check only the id but I do not want to duplicate each time the same infos name and company. Commented Jul 11, 2018 at 12:50
  • 2
    Your desired output is invalid. "Google" cannot be followed by an array. That array must be assigned to a new property. Commented Jul 11, 2018 at 12:52
  • Once again, I suggest you rethink you sql query. If you show me the table structure, I might help you. Commented Jul 11, 2018 at 12:53

1 Answer 1

3

This should be something like what you're looking for:

foreach ($arr as $a) {
    if (empty($resp[$a['id']])) {
        $resp[$a['id']] = [
            'id' => $a['id'],
            'name' => $a['name'],
            'company' => $a['company'],
            'data' => []
        ];
    }
    $resp[$a['id']]['data'][] = [
        'date' => $a['date'],
        'total' => $a['total']
    ];
}

This is assuming that you only care about grouping the date and total into a repeated company and name.

Sign up to request clarification or add additional context in comments.

6 Comments

Exactly what I needed. Thanks.
How can I remove the first level of information? [90] => Array... and start directly with ( [KDX_Id] => 90 ?
What does the KDX_Id mean? Do you just want your array to start at 0 instead of the id?
Yes. To make it minimal.
You can use array_values(). After the loop, you can call $resp = array_values($resp).
|

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.