2

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.

So if $startpos = 0, the desired print result would be a, b, c, d, e, f, g

if $startpos = 2, the desired print result would be c, d, e, f, g, a, b

if $startpos = 5, the desired print result would be 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. Anyone could please give me a suggestion?

0

3 Answers 3

5

You could use array_slice function with array_merge function as following :

$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"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, buddy ;)
2

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. 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"]

1 Comment

@b1919676 my pleasure
0

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

o/p : d e f g a b c

$startpos : 0

o/p : a b c d e f g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.