本文整理汇总了PHP中Functional\Exceptions\InvalidArgumentException::assertValidArrayKey方法的典型用法代码示例。如果您正苦于以下问题:PHP InvalidArgumentException::assertValidArrayKey方法的具体用法?PHP InvalidArgumentException::assertValidArrayKey怎么用?PHP InvalidArgumentException::assertValidArrayKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Functional\Exceptions\InvalidArgumentException
的用法示例。
在下文中一共展示了InvalidArgumentException::assertValidArrayKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: group
/**
* Groups a collection by index returned by callback.
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
*/
function group($collection, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$groups = [];
foreach ($collection as $index => $element) {
$groupKey = $callback($element, $index, $collection);
InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__);
if (!isset($groups[$groupKey])) {
$groups[$groupKey] = [];
}
$groups[$groupKey][$index] = $element;
}
return $groups;
}
示例2: group
/**
* Groups a collection by index returned by callback.
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
*/
function group($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$groups = array();
foreach ($collection as $index => $element) {
$groupKey = call_user_func($callback, $element, $index, $collection);
InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__);
if (!isset($groups[$groupKey])) {
$groups[$groupKey] = array();
}
$groups[$groupKey][$index] = $element;
}
return $groups;
}