-1

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
        )

)
codex
  • 446
  • 1
  • 7
  • 17
  • 2
    Sounds fair enough. What efforts have you made so far? – Qirel Mar 12 '19 at 08:59
  • 1
    Just get rid of `dev_priority` and use the intrinsic order of the items in the array…? Or, if you need the `dev_priority` property to mirror that: move the actual item in the array, and then iterate over the array once to set `dev_priority` to the value of index… – deceze Mar 12 '19 at 09:03
  • Does the reassignment have any order? – nice_dev Mar 12 '19 at 09:09
  • Why dont't you assign the new priority and then sort the array based on the priority. Take a look at this question: https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value – Jnt0r Mar 12 '19 at 09:14
  • @vivek_23 yes in ascending order. – codex Mar 12 '19 at 09:33
  • @codex Ok. You said `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.`. So, do you assign this, or you want it to get assigned automatically? – nice_dev Mar 12 '19 at 09:52
  • @vivek_23 As i said, want to create a function where id and dev_priority will pass and this priority assigned to given id and rest of the tasks "dev_priority" would be automatically reassigned and arrange in ascending order – codex Mar 12 '19 at 09:57

1 Answers1

0
<?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).
nice_dev
  • 17,053
  • 2
  • 21
  • 35