复制CodeIgniter新版的array_group_by辅助函数
创始人
2024-11-13 04:35:22
0

很需要php数组的group_by功能,发现codeIgniter4.5新版中已有这个辅助函数,但我用的codeIgniter4.14没有,又不想升级php等一系列东西,就想把把codeIgniter4.5中array_group_by函数复制过来用。

先试着把新版本的array_helper文件及该目录下有个Array子目录一起复制过来,但不行,于是就把这个函数相关的内容复制过来。

这个函数还引用到function arrayAttachIndexedValue,也要复制过来。

以下是添加到array_helper最后的内容

 /**      * Recursively attach $row to the $indexes path of values found by      * `dot_array_search()`.      *      * @used-by groupBy()      */     function arrayAttachIndexedValue(         array $result,         array $row,         array $indexes,         bool $includeEmpty     ): array {         if (($index = array_shift($indexes)) === null) {             $result[] = $row;              return $result;         }          $value = dot_array_search($index, $row);          if (! is_scalar($value)) {             $value = '';         }          if (is_bool($value)) {             $value = (int) $value;         }          if (! $includeEmpty && $value === '') {             return $result;         }          if (! array_key_exists($value, $result)) {             $result[$value] = [];         }          $result[$value] = arrayAttachIndexedValue($result[$value], $row, $indexes, $includeEmpty);          return $result;     }  if (! function_exists('array_group_by')) {     /**      * Groups all rows by their index values. Result's depth equals number of indexes      *      * @used-by array_group_by()      *      * @param array $array        Data array (i.e. from query result)      * @param array $indexes      Indexes to group by. Dot syntax used. Returns $array if empty      * @param bool  $includeEmpty If true, null and '' are also added as valid keys to group      *      * @return array Result array where rows are grouped together by indexes values.      */     function array_group_by(array $array, array $indexes, bool $includeEmpty = false): array     {         if ($indexes === []) {             return $array;         }          $result = [];          foreach ($array as $row) {             $result = arrayAttachIndexedValue($result, $row, $indexes, $includeEmpty);         }          return $result;     } }

相关内容

热门资讯

秒懂教程“微信链接牛牛群房卡怎... 随意玩是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享...
房卡必备教程“金花大厅房卡链接... 微信网页牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房...
金花大厅链接房卡怎么弄的/微信... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
一分钟推荐“微信金花房卡怎样购... 新神盾是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享...
房卡必备教程“微信金花链接在哪... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
微信链接炸金花房卡开科技/微信... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
安卓系统游戏昵称推荐,昵称背后... 亲爱的玩家们,你是不是在为你的安卓系统游戏昵称发愁呢?别急,今天我就来给你支支招,让你在游戏中脱颖而...
金花链接房卡怎么创建房间/创建... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
微信牛牛链接怎么制作/微信金花... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
正版授权“微信拼三张房卡怎么获... 新祥心牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
安卓系统与苹果的区别,系统差异... 你有没有想过,为什么有些人偏爱安卓手机,而有些人却对苹果爱不释手呢?这其中的奥秘,可不仅仅是因为外观...
微信炸金花房卡在哪里充/微信链... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
去哪儿买微信金花房卡/创建金花... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
金花客服代理房卡获取方式/微信... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
炸金花微信建群自己开房卡/牛牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
ia实测“微信炸金花房卡怎么开... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
购买斗牛房卡联系方式/微信链接... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
房卡必备教程“牛牛房卡购买渠道... 新乐游是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...
斗牛房卡怎么购买/微信牛牛房卡... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
微信上玩炸金花充值方式/牛牛金... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...