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);
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 24, 2014 at 20:00
\$\endgroup\$
0

2 Answers 2

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...
}
answered Mar 24, 2014 at 20:44
\$\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\$ Commented Mar 24, 2014 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\$ Commented Mar 24, 2014 at 21:09
  • \$\begingroup\$ The latter, one array with 18 different orders based on team ID. \$\endgroup\$ Commented Mar 25, 2014 at 15:09
  • \$\begingroup\$ @Conor In that case see my edit. \$\endgroup\$ Commented Mar 25, 2014 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);
answered May 2, 2014 at 21:28
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.