3
\$\begingroup\$

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);

}
\$\endgroup\$
0
1
\$\begingroup\$

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...
}
\$\endgroup\$
4
  • \$\begingroup\$ I'm just trying to loop 18 arrays but have different starting arrays based on team value. So for example, $team = 5 then output $array = array(array(5),array(6),array(7),array(8),array(9),array(10),array(11),array(12),array(13),array(14),array(15),array(16),array(17),array(18),array(1),array(2),array(3),array(4)); \$\endgroup\$ – Conor Mar 24 '14 at 21:01
  • \$\begingroup\$ Do you have 18 different starting arrays? Or are you taking the same array and doing the above for $team = 1, $team = 2, $team = 3, etc? \$\endgroup\$ – David Harkness Mar 24 '14 at 21:09
  • \$\begingroup\$ The latter, one array with 18 different orders based on team ID. \$\endgroup\$ – Conor Mar 25 '14 at 15:09
  • \$\begingroup\$ @Conor In that case see my edit. \$\endgroup\$ – David Harkness Mar 25 '14 at 20:24
0
\$\begingroup\$

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);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.