dtll2016 2019-03-12 08:57
浏览 76
已采纳

更改数组中键的值并重新排列整个多维数组

i have an array below

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => task 1
            [tech_user_id] => 1
            [dev_priority] => 1
        )

    [1] => Array
        (
            [id] => 2
            [title] => task 2
            [tech_user_id] => 1
            [dev_priority] => 2
        )

    [2] => Array
        (
            [id] => 3
            [title] => task 3
            [tech_user_id] => 1
            [dev_priority] => 3
        )

    [3] => Array
        (
            [id] => 4
            [title] => task 4
            [tech_user_id] => 1
            [dev_priority] => 4
        )

)

I want to change the priority of a task and rearrange the whole array.

Eg: if i want to change the dev_priority of task title = "task 3" from 3 to 1, then dev_priority of "task 1" should be 2 and dev_priority for "task 2" should be 3.

want to write a function rearrange where we pass $id and $set_priority and $set_priority should assigned against the given $id and whole array rearrange based on dev_priority.

rearrange($id, $set_priority) {
    // ...
}

Expected Output :

Array
(
    [0] => Array
        (
            [id] => 3
            [title] => task 3
            [tech_user_id] => 1
            [dev_priority] => 1
        )
    [1] => Array
        (
            [id] => 1
            [title] => task 1
            [tech_user_id] => 1
            [dev_priority] => 2
        )

    [2] => Array
        (
            [id] => 2
            [title] => task 2
            [tech_user_id] => 1
            [dev_priority] => 3
        )


    [3] => Array
        (
            [id] => 4
            [title] => task 4
            [tech_user_id] => 1
            [dev_priority] => 4
        )

)
  • 写回答

1条回答 默认 最新

  • duandunzhuo3234 2019-03-12 12:30
    关注
    <?php 
    
    function reArrange(&$result,$id,$new_dev_priority){
        $curr_index  = array_search($id,array_column($result,"id"),true);
        $limit_index = array_search($new_dev_priority,array_column($result,"dev_priority"),true);
    
        $process_node = $result[$curr_index];
        $curr_dev_priority = $process_node['dev_priority'];
        if($curr_dev_priority === $new_dev_priority) return; // return if same priority was assigned.    
        $offset = $curr_dev_priority > $new_dev_priority ? -1 : 1;
    
        /* rearrange by relocating elements to a new location(this is a minimized shift) */
        while($curr_index != $limit_index){
            $result[$curr_index] = $result[$curr_index + $offset];
            $result[$curr_index]['dev_priority'] = $result[$curr_index]['dev_priority'] - $offset;        
            $curr_index += $offset;
        }  
    
        $process_node['dev_priority'] = $new_dev_priority; // assign new dev priority
        $result[$curr_index] = $process_node;
    }
    

    Demo: https://3v4l.org/nggja

    • We get the current index of the process and the index of the process which has our desired new dev priority.
    • We do this because we only want to shift elements from our current location till our desired location. Shifting irrelevant processes out of this range is trivial.
    • We define a variable called offset which just determines the direction of shift. -1 for up and 1 for down.
    • Time complexity: O(n) (in worst case), Space Complexity: O(1).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊