Array offset without losing values

Posted on

Problem

After looking through PHP.net documentation for hours, is this the best way to offset an array from start to finish by using array_slice() with array_merge()?

For example: $array = array(1,2,3,4) to offset by 2 to get return $array = array(3,4,1,2).

Here’s the code I’m using it in:

    $team = 2;
    $course = array(
    array('title'=>1,  'hole'=> 'h01', 'shot1'=>'value="-3"', 'shot2'=>'value="-2"', 'shot3'=>'value="-1"', 'shot4'=>'value="0"', 'shot5'=>'disabled="disabled"'),
    array('title'=>2,  'hole'=> 'h02', 'shot1'=>'value="-4"', 'shot2'=>'value="-3"', 'shot3'=>'value="-2"', 'shot4'=>'value="-1"', 'shot5'=>'value="0"'),
    array('title'=>3,  'hole'=> 'h03', 'shot1'=>'value="-3"', 'shot2'=>'value="-2"', 'shot3'=>'value="-1"', 'shot4'=>'value="0"', 'shot5'=>'disabled="disabled"'),
    array('title'=>4,  'hole'=> 'h04', 'shot1'=>'value="-2"', 'shot2'=>'value="-1"', 'shot3'=>'value="0"', 'shot4'=>'disabled="disabled"', 'shot5'=>'disabled="disabled"'),
    array('title'=>5,  'hole'=> 'h05', 'shot1'=>'value="-4"', 'shot2'=>'value="-3"', 'shot3'=>'value="-2"', 'shot4'=>'value="-1"', 'shot5'=>'value="0"')
    );

    $array1 = array_slice($course, $team);
   $array2 = array_slice($course, 0, $team);
   $merged = array_merge($array1, $array2);

}

Solution

If you don’t mind modifying the original array, you can shorten the (perfectly adequate code you have) with this:

$head = array_splice($course, 0, $team);  // remove and return first $team elements
$merged = array_merge($course, $head);    // append them to the end

You should be able to omit the temporary $head array by inserting the array_splice call into the array_merge call at the cost of a little code clarity. Try it out to make sure the order of operations is correct.

$merged = array_merge($course, array_splice($course, 0, $team));

Edit: If you’re doing this once per team you can use a loop with array_shift to remove the first element and array_push to place it at the end:

for ($team = 0; $team < $numTeams; $team++) {
    array_push($course, array_shift($course));
    // use $course...
}

Here’s what worked best for me.

$team = 2;
$start = $team - 1;
$course = array(
array('title'=>1,  'hole'=> 'h01', 'shot1'=>'value="-3"', 'shot2'=>'value="-2"', 'shot3'=>'value="-1"', 'shot4'=>'value="0"', 'shot5'=>'disabled="disabled"'),
array('title'=>2,  'hole'=> 'h02', 'shot1'=>'value="-4"', 'shot2'=>'value="-3"', 'shot3'=>'value="-2"', 'shot4'=>'value="-1"', 'shot5'=>'value="0"'),
array('title'=>3,  'hole'=> 'h03', 'shot1'=>'value="-3"', 'shot2'=>'value="-2"', 'shot3'=>'value="-1"', 'shot4'=>'value="0"', 'shot5'=>'disabled="disabled"'),
array('title'=>4,  'hole'=> 'h04', 'shot1'=>'value="-2"', 'shot2'=>'value="-1"', 'shot3'=>'value="0"', 'shot4'=>'disabled="disabled"', 'shot5'=>'disabled="disabled"'),
array('title'=>5,  'hole'=> 'h05', 'shot1'=>'value="-4"', 'shot2'=>'value="-3"', 'shot3'=>'value="-2"', 'shot4'=>'value="-1"', 'shot5'=>'value="0"')
);

$slice1 = array_slice($course, $start);
$slice2 = array_slice($course, 0, $start);
$merged = array_merge($slice1, $slice2);

Leave a Reply

Your email address will not be published. Required fields are marked *