本文整理匯總了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;
}