1

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 -_-

9
  • you mean a built-in function that does your special business logic for your sorting? obviously there is none, figure it out first on how would you sort your array (the criteria) and start coding Commented Jun 1, 2018 at 8:25
  • can you give me some tips? Commented Jun 1, 2018 at 8:27
  • 1
    What is the actual problem with keeping it the way it is? Commented Jun 1, 2018 at 8:28
  • @Andreas that could be asked of 75% of Stack overflow questions. Commented Jun 1, 2018 at 8:32
  • @PhilipCouling Yes. And most of the times there is no good reason, and you help OP with finding a much more optimized solution Commented Jun 1, 2018 at 8:35

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest method is to remove the key from the original array and push it to the end.

like this :

$keyToMove = 'current_files';
if (array_key_exists($keyToMove, $arrayToSort)) {
    $tmp = $arrayToSort[$keyToMove]; // extract the key to move
    unset($arrayToSort[$keyToMove]); // unset the key from the array
    $arrayToSort[$keyToMove] = $tmp; // add the saved data in the end
}

Then if you have a multidimentionnal array to "sort" you will have to run this recursively.

Comments

1

Well seeing as you already know the key of the item you want to move, things can be pretty simple with a recursive function. You use the '&' simple to pass the array in as reference.

function moveCurrentFiles(&$array) {
    //first move current_files to the end of the array
    if (isset($array["current_files"])) {
        $currentFiles = $array["current_files"];
        //unset then reset value to "move" it to the end
        unset($array["current_files"]);
        $array["current_files"] = $currentFiles;
    }

    //loop through array items to check if any of them are arrays
    foreach($array as &$value) {
         if (is_array($value)) {
             //recursively call this function on that array
             moveCurrentFiles($value);
         }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.