php 用于向左和向右旋转数组元素的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用于向左和向右旋转数组元素的功能相关的知识,希望对你有一定的参考价值。
<?php
function rotate_array_left($array, $rotations){
for ($r=0; $r<$rotations; $r++){
array_push($array, array_shift($array));
}
return $array;
}
function rotate_array_right($array, $rotations){
for ($r=0; $r<$rotations; $r++){
array_unshift($array, array_pop($array));
}
return $array;
}
$n = 1; //Number of rotations
$a = array(1,2,3,4,5);
echo "Original Array with $n rotations ";
foreach($a as $value){
echo $value . " ";
}
echo "<br>";
//Rotate Array Left
$b = rotate_array_left($a,$n);
foreach($b as $element){
echo $element . " ";
}
print_r('<br>');
//Rotate Array Right
$b = rotate_array_right($a,$n);
foreach($b as $element){
echo $element . " ";
}
以上是关于php 用于向左和向右旋转数组元素的功能的主要内容,如果未能解决你的问题,请参考以下文章