doujiepin5547 2018-06-01 08:22
浏览 50
已采纳

递归函数将多维数组中的特定键移动到其级别的底部

I'm looking for a php (sort?)function which move a specific key ('current_files') to the bottom of its array level.

I have sth like that:

[A] => [
     [current_files] => [
           [0] => ...
           [1] => ...  
     ]
     [D] => [
          [G] => [...]   
          [current_files] => [...]
          [B] => [...]
     ]
]
[current_files] => [...]
[K] => [...]

I need this:

[A] => [
     [D] => [
          [G] => [...]   
          [B] => [...]
          [current_files] => [...]
     ]
     [current_files] => [
           [0] => ...
           [1] => ...  
     ]
]
[K] => [...]
[current_files] => [...]

I know that i need a recursive function like arr_multisort but i dont understand it -_-

  • 写回答

3条回答 默认 最新

  • dpauxqt1281 2018-06-01 09:00
    关注

    Try this

    Written a common function just call that function to move down any key by passing the specific key as argument.

    function sortArrayByKey(&$array, $search_key) {
        $searched_key_arr = array();
        foreach ($array as $k => &$values) {
            if (array_key_exists($search_key, $values)) {
                sortArrayByKey($values, $search_key);
            } else if ($k == $search_key) {
                $searched_key_arr[$k] = $values;
                unset($array[$k]);
            }
        }
        if (!empty($searched_key_arr)) {
            foreach ($searched_key_arr as $key => $val) {
                $array[$key] = $val;
            }
        }
        return $array;
    }
    
    $arr = $this->sortArrayByKey($data, 'current_files'); //$data is your input array
    print_r($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题