/** * @test */ public function it_should_group_by_key_deep() { $items = [['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]], ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]], ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]]]; $collection = new Map($items); $grouped = $collection->groupBy(function ($element) { return $element['thing']['parent_id']; }); $expected = [10 => [['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]], ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]]], 11 => [['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]]]]; $this->assertEquals($expected, $grouped->toArray()); }