简体   繁体   English

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

[英]PHP circular loop through array from n to n-1 element

Let's say I have this array: 假设我有这个数组:

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

And I have a start indicator, $startpos , whose possible value can be any value from 0 to myArray's number of elements. 我有一个开始指示符$startpos ,其可能的值可以是从0到myArray的元素数量的任何值。

So if $startpos = 0 , the desired print result would be a, b, c, d, e, f, g 因此,如果$startpos = 0 ,则所需的打印结果将是a, b, c, d, e, f, g

if $startpos = 2 , the desired print result would be c, d, e, f, g, a, b 如果$startpos = 2 ,则所需的打印结果为c, d, e, f, g, a, b

if $startpos = 5 , the desired print result would be f, g, a, b, c, d, e 如果$startpos = 5 ,则所需的打印结果为f, g, a, b, c, d, e

I've been searching a php built-in or custom function through SO (similar question at Treat an array as circular array when selecting elements - PHP ) and having a look to http://www.w3schools.com/php/php_ref_array.asp but I'm not getting the desired result. 我一直在通过SO搜索一个php内置或自定义函数(类似问题, 在选择元素时将数组视为圆形数组 - PHP )并查看http://www.w3schools.com/php/php_ref_array。 asp,但我没有得到理想的结果。 Anyone could please give me a suggestion? 有人可以给我一个建议吗?

You could use array_slice function with array_merge function as following : 您可以将array_slice函数与array_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);

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"
}

demo 演示

 <?php
      $myArray = array(a, b, c, d, e, f, g);
      $startpos = 3;
      $o = f($myArray, $startpos);
      echo json_encode($o);

      function f($myArray, $startpos)
      {
        $o = array();
        $l = count($myArray);
        array_walk($myArray, function($v, $k) use(&$o, $l, $startpos)
        {
          $o[($k + $l - $startpos) % $l] = $v;
        });
        ksort($o);
        return ($o);
      }

or use foreach method. 或使用foreach方法。 demo 演示

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

outpur: ["d","e","f","g","a","b","c"] outpur: ["d","e","f","g","a","b","c"]

If you are looking for a simple logic you can go for the below codepiece: 如果您正在寻找一个简单的逻辑,您可以使用下面的代码:

$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++;
    }
}

Output : 输出:

$startpos : 3 $ startpos:3

o/p : defgabc o / p:defgabc

$startpos : 0 $ startpos:0

o/p : abcdefg o / p:abcdefg

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

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