Reputation: 5471
I was wondering how I could change the ordering of the following array:
Array
(
[1] => Array
(
[0] => stdClass Object
(
[id] => 120
[hteam] => Heatherton
[ateam] => Dingley
[round] => 1
[catid] => Seniors
[date] => Saturday 14th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
[1] => stdClass Object
(
[id] => 121
[hteam] => Heatherton
[ateam] => Dingley
[round] => 1
[catid] => Reserves
[date] => Saturday 14th April, 2012
[time] => 11:45am
[venue] => Heatherton Recreational Reserve
)
)
[2] => Array
(
[0] => stdClass Object
(
[id] => 122
[hteam] => Clayton
[ateam] => Heatherton
[round] => 2
[catid] => Seniors
[date] => Saturday 21st April, 2012
[time] => 12:00am
[venue] => Clayton Reserve
)
[1] => stdClass Object
(
[id] => 139
[hteam] => Clayton
[ateam] => Heatherton
[round] => 2
[catid] => Reserves
[date] => Saturday 21st April, 2012
[time] => 12:00am
[venue] => Clayton Reserve
)
)
[3] => Array
(
[0] => stdClass Object
(
[id] => 123
[hteam] => Heatherton
[ateam] => St Pauls
[round] => 3
[catid] => Seniors
[date] => Saturday 28th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
[1] => stdClass Object
(
[id] => 140
[hteam] => Heatherton
[ateam] => St Pauls
[round] => 3
[catid] => Reserves
[date] => Saturday 28th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
)
[4] => Array
(
[0] => stdClass Object
(
[id] => 124
[hteam] => Mordialloc
[ateam] => Heatherton
[round] => 4
[catid] => Seniors
[date] => Saturday 5th May, 2012
[time] => 02:00pm
[venue] => Ben Kavanagh Reserve
)
[1] => stdClass Object
(
[id] => 141
[hteam] => Mordialloc
[ateam] => Heatherton
[round] => 4
[catid] => Reserves
[date] => Saturday 5th May, 2012
[time] => 11:45am
[venue] => Ben Kavanagh Reserve
)
)
)
From ^^ to this:
Array
(
[3] => Array
(
[0] => stdClass Object
(
[id] => 123
[hteam] => Heatherton
[ateam] => St Pauls
[round] => 3
[catid] => Seniors
[date] => Saturday 28th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
[1] => stdClass Object
(
[id] => 140
[hteam] => Heatherton
[ateam] => St Pauls
[round] => 3
[catid] => Reserves
[date] => Saturday 28th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
)
[4] => Array
(
[0] => stdClass Object
(
[id] => 124
[hteam] => Mordialloc
[ateam] => Heatherton
[round] => 4
[catid] => Seniors
[date] => Saturday 5th May, 2012
[time] => 02:00pm
[venue] => Ben Kavanagh Reserve
)
[1] => stdClass Object
(
[id] => 141
[hteam] => Mordialloc
[ateam] => Heatherton
[round] => 4
[catid] => Reserves
[date] => Saturday 5th May, 2012
[time] => 11:45am
[venue] => Ben Kavanagh Reserve
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 120
[hteam] => Heatherton
[ateam] => Dingley
[round] => 1
[catid] => Seniors
[date] => Saturday 14th April, 2012
[time] => 12:00am
[venue] => Heatherton Recreational Reserve
)
[1] => stdClass Object
(
[id] => 121
[hteam] => Heatherton
[ateam] => Dingley
[round] => 1
[catid] => Reserves
[date] => Saturday 14th April, 2012
[time] => 11:45am
[venue] => Heatherton Recreational Reserve
)
)
[2] => Array
(
[0] => stdClass Object
(
[id] => 122
[hteam] => Clayton
[ateam] => Heatherton
[round] => 2
[catid] => Seniors
[date] => Saturday 21st April, 2012
[time] => 12:00am
[venue] => Clayton Reserve
)
[1] => stdClass Object
(
[id] => 139
[hteam] => Clayton
[ateam] => Heatherton
[round] => 2
[catid] => Reserves
[date] => Saturday 21st April, 2012
[time] => 12:00am
[venue] => Clayton Reserve
)
)
)
So in this example, the array starts on number 3, goes to 4, then back to number 1 again.. if that makes sense?
Upvotes: -1
Views: 2646
Reputation: 47982
The depth/type of each element is irrelevant to this task. If you know what zero-based position should become the new starting element, pass that integer into the following script. Demo
$array = [1, 2, 3, 4];
$startAtPos = 2;
var_export(
array_merge(
array_splice($array, $startAtPos),
$array
)
);
Output:
array (
0 => 3,
1 => 4,
2 => 1,
3 => 2,
)
Upvotes: 0
Reputation: 8218
You could use the array_keys() function to extract all the keys, then make a map from old keys to new keys, and then finally construct a new array using the new keys, with values obtained from the old array using the new key-old key map.
Upvotes: 1
Reputation: 3529
You can use usort php function that allows to define your own sort functions.
Upvotes: 2