本文整理汇总了PHP中Cake\Collection\Collection::groupBy方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::groupBy方法的具体用法?PHP Collection::groupBy怎么用?PHP Collection::groupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Collection\Collection
的用法示例。
在下文中一共展示了Collection::groupBy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: index
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$releases = $this->Releases->find('all')->all();
$collection = new Collection($releases);
$attribute_names = array_unique($collection->extract('attribute_name')->toArray());
$idps = array_unique($collection->extract('idp')->toArray());
$releasesByIdp = $collection->groupBy('idp')->toArray();
foreach ($idps as $idp) {
foreach ($attribute_names as $attribute) {
$releasesByIdPbyAttribute = $collection->match(['idp' => $idp, 'attribute_name' => $attribute]);
$temp_result = $releasesByIdPbyAttribute->countBy(function ($result) {
return strtolower($result->validated) == 'fail' ? 'fail' : 'pass';
});
$results[$idp][$attribute] = $temp_result->toArray();
}
}
# My attributes
$persistentid_array = preg_split('/!/', $this->request->env('persistent-id'));
$persistentid = end($persistentid_array);
$myAttributesTemp = $this->Releases->find()->andWhere(['idp' => $this->request->env('Shib-Identity-Provider'), 'persistentid' => $persistentid])->all();
$myAttributesCollection = new Collection($myAttributesTemp);
$myAttributes = $myAttributesCollection->groupBy('attribute_name')->toArray();
$this->set(compact('myAttributes'));
$this->set('_serialize', ['myAttributes']);
$this->set(compact('results'));
$this->set('_serialize', ['results']);
$this->set(compact('idps'));
$this->set('_serialize', ['idps']);
$this->set(compact('attribute_names'));
$this->set('_serialize', ['attribute_names']);
}
示例2: testGroupByDeepKey
/**
* Tests grouping by a deep key
*
* @return void
*/
public function testGroupByDeepKey()
{
$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 Collection($items);
$grouped = $collection->groupBy('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, iterator_to_array($grouped));
}