Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从n到n-1元素的数组循环_Php_Arrays_Loops - Fatal编程技术网

PHP从n到n-1元素的数组循环

PHP从n到n-1元素的数组循环,php,arrays,loops,Php,Arrays,Loops,假设我有一个数组: $myArray = array(a, b, c, d, e, f, g); 我有一个开始指示器,$startpos,它的可能值可以是从0到myArray元素数的任意值 因此,如果$startpos=0,所需的打印结果将是a、b、c、d、e、f、g 如果$startpos=2,所需的打印结果将是c、d、e、f、g、a、b 如果$startpos=5,所需的打印结果将是f、g、a、b、c、d、e 我一直在搜索一个php内置函数或自定义函数,通过SO(类似的问题在)和查看,但我

假设我有一个数组:

$myArray = array(a, b, c, d, e, f, g);
我有一个开始指示器,
$startpos
,它的可能值可以是从0到myArray元素数的任意值

因此,如果
$startpos=0
,所需的打印结果将是
a、b、c、d、e、f、g

如果
$startpos=2
,所需的打印结果将是
c、d、e、f、g、a、b

如果
$startpos=5
,所需的打印结果将是
f、g、a、b、c、d、e


我一直在搜索一个php内置函数或自定义函数,通过SO(类似的问题在)和查看,但我没有得到想要的结果。谁能给我一个建议

您可以使用
array\u slice
函数和
array\u merge
函数,如下所示:

$myArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
$startpos = 2;


$output = array_merge(
                 array_slice($myArray,$startpos),
                 array_slice($myArray, 0, $startpos)
                    ); 
var_dump($output);
输出:

array(7) {
  [0]=>
  string(1) "c"
  [1]=>
  string(1) "d"
  [2]=>
  string(1) "e"
  [3]=>
  string(1) "f"
  [4]=>
  string(1) "g"
  [5]=>
  string(1) "a"
  [6]=>
  string(1) "b"
}


如果您正在寻找一个简单的逻辑,您可以选择以下代码段:

$myArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
$startpos = <any_position>;
$dummy_startpos = $startpos;  //keeping safe startpos for circular calculation
$initialpos = 0;

//loop to print element from startpos to end
while($dummy_startpos < count($myArray))
{
    echo $myArray[$dummy_startpos] . ' ';
    $dummy_startpos++;
}

//if startpos is not initial position
//start from first and print element till startpos
if($startpos > 0)
{
    while($elementleft < $startpos)
    {
        echo $myArray[$elementleft] . ' ';
        $elementleft++;
    }
}
$myArray=array('a','b','c','d','e','f','g');
$startpos=;
$dummy_startpos=$startpos//保持循环计算的安全起始点
$initialpos=0;
//循环打印从startpos到end的元素
而($dummy_startpos0)
{
而($elementleft<$startpos)
{
echo$myArray[$elementleft]。';
$elementleft++;
}
}
输出:

$startpos:3

o/p:d e f g a b c

$startpos:0

o/p:a b c d e f g


谢谢你,伙计;)@b1919676我的荣幸
<?php
  $myArray = array(a, b, c, d, e, f, g);
  $startpos = 3;
  echo json_encode(f($myArray, $startpos));

  function f($myArray, $startpos)
  {
    $o = array();
    $l = count($myArray);
    foreach($myArray as $k => $v)
    {
      $o[($k + $l - $startpos) % $l] = $v;
    }
    ksort($o);
    return $o;
  }
$myArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
$startpos = <any_position>;
$dummy_startpos = $startpos;  //keeping safe startpos for circular calculation
$initialpos = 0;

//loop to print element from startpos to end
while($dummy_startpos < count($myArray))
{
    echo $myArray[$dummy_startpos] . ' ';
    $dummy_startpos++;
}

//if startpos is not initial position
//start from first and print element till startpos
if($startpos > 0)
{
    while($elementleft < $startpos)
    {
        echo $myArray[$elementleft] . ' ';
        $elementleft++;
    }
}