/**
* Author: 小破孩
* Email: 3584685883@qq.com
* Time: 2021/12/31 15:54
* @param $arr
* @param $children
* @return mixed
* Description:多维数组转二维数组
*/
public function setManyTosingle(array $arr= [], string $children= 'child'):array{
// 获取key列表
$keyLists = array_keys($arr);
// 获取数组长度
$count = count($keyLists);
foreach ($arr as $key => $value) {
if (!is_array($value)) {
return $arr;
}
if (key_exists($children, $value)) {
// 查找当前key在key列表中的key
$index = array_search($key, $keyLists);
$index++;
// 插入子数组
// 判断插入位置是否存在
if ($index >= $count) {
// 如果不存在
$arr = array_merge($arr, $value[$children]);
} else {
// 如果存在
$doing = array_splice($arr, $keyLists[$index], 0, $value[$children]);
}
// 删除之前的子数组
unset($arr[$key][$children]);
// 重新调用该方法
$arr = $this->setManyTosingle($arr, $children);
// 返回操作结果
// 如果不重新循环会出现key值错误的问题
return $arr;
}
}
return $arr;
}
版权属于:
小破孩
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论