0

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!

3
  • Looks like a typical problem for LINQ. See phplinq.codeplex.com or plinq.codeplex.com. Disclosure: I have little experience w/ PHP and none w/ these LINQ implementations (beware some seem a bit "young"). I'm just glad to see this type of features coming to more languages and abstracting out the more common programming tasks. Commented Aug 19, 2011 at 19:55
  • This seems a bit overkill for a rather simple problem, but still, thanks for the suggestion! Commented Aug 19, 2011 at 20:18
  • Agreed, if this the only use of LINQ-like constructs you may have, but, assuming some LINQ libraries for PHP are stable enough, the investment for adding this feature to your PHP environment and for some possible "learning curve" should typically pay back for itself quickly. There are so many instances where few lines of LINQ can replace long boilerplate code sections, making the program easier to read, but also faster to write. Commented Aug 19, 2011 at 20:27

1 Answer 1

1

Pseudo-code:

  1. Grab array_values from the original data set.
  2. Use array_merge to combine them all (essentially flatten)
  3. Use a usort and pop off first 5 elements
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.