I have an array of elements like this:
$data = array(1, 2, 3, 4);
How can I reorder for example, starting from second element to get 2, 3, 4, 1
; or starting from third element to get 3, 4, 1, 2
?
One solution is to use array_slice
function to separate the two portions and combine them with array_merge
:
$data = [1, 2, 3, 4];
$pos = 2;
$ordered = array_merge(
array_slice($data, $pos),
array_slice($data, 0, $pos)
);
// [3, 4, 1, 2]