2

I thought I have some understanding of arrays. But it looks like I have no understanding. Or my head don't want to work. I have arrays:

[0] => Array
    (
        [key] => Person 1
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1436821440000,12
                    )
            )
    )

[1] => Array
    (
        [key] => Person 2
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437562620000,24
                    )
            )
    )

[2] => Array
    (
        [key] => Person 3
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437080040000,10
                    )
            )
    )

[3] => Array
    (
        [key] => Person 1
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437082860000,1
                    )
            )
    )

 [4] => Array
    (
        [key] => Person 3
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437081840000,9
                    )
            )
    )

And here is what I want to achieve:

    [0] => Array
    (
        [key] => Person 1
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1436821440000,12
                        [1] => 1437082860000,1
                    )
            )
    )

[1] => Array
    (
        [key] => Person 2
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437562620000,24
                    )
            )
    )

[2] => Array
    (
        [key] => Person 3
        [values] => Array
            (
                [0] => Array
                    (
                        [0] => 1437080040000,10
                        [1] => 1437081840000,9
                    )
            )
    )

How could I remove duplicates and merge data?

2 Answers 2

1

The sample input doesn't offer any variation in the data contained in the deept subarrays, so I'll demonstrate the utility of my snippet by adding a little more complexity in the data.

Effectively, each time a new key is encountered, you declare a new row containing the key element and a values element with an empty array. To identify that key as "encountered", the new row is assigned to the result array using the key value as the first level key.

Now that the values subarray is guaranteed to be declared, an inner loop will comb through the deeper arrays and push one or more (all) deep values into the respective values subarray -- in a flat fashion.

Code: (Demo)

$array = [
    ['key' => 'Person 1', 'values' => [['1436821440000,12']]],
    ['key' => 'Person 2', 'values' => [['1437562620000,24']]],
    ['key' => 'Person 3', 'values' => [['1437080040000,10']]],
    ['key' => 'Person 1', 'values' => [['1437082860000,1'], ['1437082860000,2', '1437082860000,3']]],
    ['key' => 'Person 3', 'values' => [['1437081840000,9']]],
];

$result = [];
foreach ($array as $row) {
    if (!isset($result[$row['key']])) {
        $result[$row['key']] = ['key' => $row['key'], 'values' => []];
    }
    foreach ($row['values'] as $values) {
        array_push($result[$row['key']]['values'], ...$values);
    }
}
var_export(
    array_values($result)
);

Output:

array (
  0 => 
  array (
    'key' => 'Person 1',
    'values' => 
    array (
      0 => '1436821440000,12',
      1 => '1437082860000,1',
      2 => '1437082860000,2',
      3 => '1437082860000,3',
    ),
  ),
  1 => 
  array (
    'key' => 'Person 2',
    'values' => 
    array (
      0 => '1437562620000,24',
    ),
  ),
  2 => 
  array (
    'key' => 'Person 3',
    'values' => 
    array (
      0 => '1437080040000,10',
      1 => '1437081840000,9',
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

0
$result_arr = array();
foreach ($arr as $sub_arr) 
{
  $result_arr = array_merge($result_arr, $sub_arr);
  $result_arr = array_unique($result_arr);
}

2 Comments

I'm sorry, but unfortunately this won't work the way that you expect it to. This would completely get rid of Person 2 and would also result in the inner array values only using the last array value.
Yes, Ohgodwhy is right. And I'm getting "array to string conversion: error.

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.