Home  >  Article  >  Backend Development  >  How to implement array rotation in php? Introduction to various methods

How to implement array rotation in php? Introduction to various methods

PHPz
PHPzOriginal
2023-04-19 10:06:44549browse

PHP provides many array operation functions, one of which is the rotation operation of the array. Array rotation refers to the operation of transforming the positions of array elements according to fixed rules, so that the elements originally arranged together are dispersed in different positions after being arranged. Array rotation can be used in many scenarios, such as randomly shuffling the order of array elements, implementing polling, dividing arrays, etc.

PHP provides a variety of ways to implement array rotation. Here are three commonly used ways.

1. Use array_splice function

The array_splice function can be used to remove a subarray of specified length from an array. We can rotate the array by calling this function multiple times. Consider rotating the array $a$ right by $k$ bits, which can be achieved according to the following ideas:

  1. Take out the last $k$ elements of the array $a$ to form a subarray $P$;
  2. Delete the last $k$ elements of the array $a$ to obtain a new array $a'$;
  3. Insert the subarray $P$ to the beginning of the new array $a'$ position to form a new array $a''$.

The code is implemented as follows:

function rotateArray1($arr, $k) {
    $n = count($arr);
    $k = $k % $n; // 取模,防止$k > $n的情况
   
    $p = array_splice($arr, $n - $k, $k);
    $arr = array_merge($p, $arr);
    return $arr;
}

2. Use array_shift and array_push functions

The array_shift function can take out and return the first element of the array, and the array_push function You can add elements to the end of the array. We can rotate the array by calling these two functions in a loop. It should be noted that although this method is feasible, the time complexity is high, because each call to array_shift requires the entire array to be moved forward by one bit, so when $k$ is relatively large, the efficiency will be very low.

The code is implemented as follows:

function rotateArray2($arr, $k) {
    $k = $k % count($arr); //防止$k > count($arr)的情况
    for ($i = 0; $i < $k; $i++) {
        $elem = array_shift($arr); // 取出第一个元素
        array_push($arr, $elem);  // 将元素放入数组末尾
    }
    return $arr;
}

3. Use array subscripts to achieve rotation

In addition to using array operation functions, we can also implement array rotation by manually operating array subscripts . The specific operation is: put the element with subscript $i$ into the position with subscript $(i k)\%n$, where $n$ is the length of the array and $k$ is the number of digits for right rotation. This operation needs to be executed in a loop $n$ times.

The code is implemented as follows:

function rotateArray3($arr, $k) {
    $n = count($arr);
    $k = $k % $n;
    for ($i = 0; $i < $n; $i++) {
        $newIndex = ($i + $k) % $n;
        $newArr[$newIndex] = $arr[$i];
    }
    return $newArr;
}

The above three methods can all realize the array rotation operation. The specific method used depends on the scenario and data scale required. It should be noted that in actual use, factors such as the type and size of the array also need to be considered to avoid various abnormal situations that may lead to program errors.

The above is the detailed content of How to implement array rotation in php? Introduction to various methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn