简体   繁体   English

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

[英]Recursive function to move a specific key in a multidimensional array to the bottom of his level

I'm looking for a php (sort?)function which move a specific key ( 'current_files' ) to the bottom of its array level. 我正在寻找一个php(sort?)函数,它将一个特定的键( 'current_files' )移动到它的数组级别的底部。

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 -_- 我知道我需要像arr_multisort这样的递归函数,但我不明白它-_-

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. 然后,如果你有一个multidimentionnal数组来“排序”,你将不得不递归地运行它。

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. 您可以使用'&'simple将数组作为参考传递。

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);
         }
    }
}

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM