Rotating Array Elements to the Left in PHP
PHP provides a simple and effective way to "rotate" an array, meaning to move the first element to the last and renumber the indices accordingly. This can be achieved using the array_push() and array_shift() functions.
Example:
Suppose we have an array $numbers with the values 1, 2, 3, and 4. To rotate it to the left, we can use the following code:
<code class="php">$numbers = array(1, 2, 3, 4); array_push($numbers, array_shift($numbers)); print_r($numbers);</code>
Output:
Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 1 )
Explanation:
The above is the detailed content of How to Rotate Array Elements to the Left in PHP?. For more information, please follow other related articles on the PHP Chinese website!