0

I want to make arrays into groups by id,
but still keep the orders, that means it might have duplicate groups, like:

Group1 [id: 1]
Group2 [id: 2]
Group3 [id: 1]
....
...
..
.

Array:

array (
  0 => 
  array (
    'id' => 1,
    'message' => 'AAA',
    'sent_on' => 1582097767
  ),

  1 => 
  array (
    'id' => 2,
    'message' => 'AAAQAW',
    'sent_on' => 1582097770
  ),

  2 => 
  array (
    'id' => 2,
    'message' => 'dqwdq',
    'sent_on' => 1582097772
  ),

  3 => 
  array (
    'id' => 1,
    'message' => 'dqwdq',
    'sent_on' => 1582097773
  ),

  4 => 
  array (
    'id' => 1,
    'message' => 'wq',
    'sent_on' => 1582097773
  ),

  5 => 
  array (
    'id' => 2,
    'message' => 'd',
    'sent_on' => 1582097774
  ),

  6 => 
  array (
    'id' => 1,
    'message' => 'dew',
    'sent_on' => 1582112219
  )
)

Hoped result:

array (
    0 => 
    array (
        0 => 
        array (
          'id' => 1,
          'message' => 'AAA',
          'sent_on' => 1582097767

        ),
    ),

    1 => 
    array (
        0 => 
        array (
          'id' => 2,
          'message' => 'AAAQAW',
          'sent_on' => 1582097770

        ),
        1 => 
        array (
          'id' => 2,
          'message' => 'dqwdq',
          'sent_on' => 1582097772

        )
    ),

    2 => 
    array (
        0 => 
        array (
          'id' => 1,
          'message' => 'dqwdq',
          'sent_on' => 1582097773

        ),
        1 => 
        array (
          'id' => 1,
          'message' => 'wq',
          'sent_on' => 1582097773

        )
    ),

    3 => 
    array (
        0 => 
        array (
          'id' => 2,
          'message' => 'd',
          'sent_on' => 1582097774

        )
    ),

    4 => 
    array (
        0 => 
        array (
          'id' => 1,
          'message' => 'dew',
          'sent_on' => 1582112219

        )
    )
)
2
  • aaaand what seems to be an issue? Commented Feb 19, 2020 at 16:43
  • @matiit Uh, yes, I only know how to make the array into only two groups like id=1, id=2, but in my hope it need to have five groups Commented Feb 19, 2020 at 16:45

1 Answer 1

1

Yes, it could be done pretty easy by using array_reduce:

$lastCheckedItem = ['id' => 0];

$finalArray = array_reduce($array, static function(array $carry, array $item) use (&$lastCheckedItem) {
    if ($lastCheckedItem['id'] === $item['id'] && !empty($carry)) {
        $lastKey = array_key_last($carry);
        $carry[$lastKey][] = $item;
    }
    else {
        $carry[] = [$item];
    }

    $lastCheckedItem = $item;

    return $carry;
}, []);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot !!! Great answer !! By the way, why the array name is $carry lol
yw. Well, because it carries the values set on each iteration until the loop is ended.
Got it. It made me laugh due to it is my english nickname from my dad.
yeah.. I noticed it since you asked about it :)

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.