Reputation: 13257
I have the following array:
Array
(
[medium_priority] => Array
(
[0] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 60
)
)
[high_priority] => Array
(
[0] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 95
)
[1] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 85
)
[2] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 80
)
)
[low_priority] => Array
(
[0] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 20
)
[1] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 30
)
)
)
Using PHP, I want to get an array consisting of the five values with the highest weight
, like this:
Array
(
[0] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 95
)
[1] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 85
)
[2] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 80
)
[3] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 60
)
[4] => Array
(
[module] => a
[block] => b
[message] => c
[weight] => 30
)
)
Of course, this could be accomplished by iterating over the array using a loop to 'flatten' the array and then sort it usort
, but I think there should be an easier and faster way.
However, I can't figure out what that would be.
I hope you can help me!
Upvotes: 0
Views: 85
Reputation: 101614
Pseudo-code:
array_values
from the original data set.array_merge
to combine them all (essentially flatten)Upvotes: 1