Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:07:01+00:00 2026-06-14T06:07:01+00:00

I’m receiving an array from a database – I have no control over what

  • 0

I’m receiving an array from a database – I have no control over what pieces of data are sent and in what order. This is what it currently looks like:

Array
(
    [itemCode] => Array
        (
            [0] => Array
                (
                    [code] => P
                    [descShort] => Pepperoni
                )

            [1] => Array
                (
                    [code] => G
                    [descShort] => Green Peppers
                )

            [2] => Array
                (
                    [code] => n
                    [descShort] => No Sauce
                )

            [3] => Array
                (
                    [code] => x
                    [descShort] => No Cheese
                )

            [4] => Array
                (
                    [code] => 
                    [descShort] => Regular Cheese
                )

            [5] => Array
                (
                    [code] => 
                    [descShort] => Regular Sauce
                )

        )

)

In actual practice, there can be any number of elements before the No Sauce option (currently at index 3, but not always that way.) What the client wants is for the Cheese and Sauce items to always be at the end of the list and ordered this way: Regular Cheese, No Cheese, Regular Sauce, No Sauce.

Again, keeping in mind that I have no control over how the array is initially created, and that there could be any number of other elements both before and between the elements in question, how can I make this happen? Something else to worry about is that at some point, there may be other options they want to include in this re-ordering (they may add an option for Extra Cheese and Extra Sauce, for example, and want them to be in specific positions as well.)

Added var_export

array (
  'itemCode' => 
  array (
    0 => 
    array (
      'code' => 'P',
      'descShort' => 'Pepperoni',
    ),
    1 => 
    array (
      'code' => 'G',
      'descShort' => 'Green Peppers',
    ),

    2 => 
    array (
      'code' => 'n',
      'descShort' => 'No Sauce',
    ),
    3 => 
    array (
      'code' => 'x',
      'descShort' => 'No Cheese',
    ),
    4 => 
    array (
      'code' => '',
      'descShort' => 'Regular Cheese',
    ),
    5 => 
    array (
      'code' => '',
      'descShort' => 'Regular Sauce',
    ),
  ),
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:07:02+00:00Added an answer on June 14, 2026 at 6:07 am

    As it has been already commented, the usort function can be used for that. But this is only the start as it needs the compare function.

    That is also not that hard, because we can just create one. However, it’s crucial to understand how it works:

    The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

    Okay, that’s not a blocker but turns the actual question, how to find out which one is above which one? And how to do that extensible?

    There are two type of values: Either the ones that do not need any ordering or the ones that need ordering. Let’s first define an array for those that need ordering. And to make it clear for what that ordering is, name the key:

    $order    = 'descShort';
    $ordering = ['Regular Cheese', 'No Cheese', 'Regular Sauce', 'No Sauce'];
    

    Now inside a compare function, you can look up if for the A/B values an entry exists within the ordering. If it does not exists, then this needs no ordering. If it exists, this needs ordering.

    These two cases are extended because you also can have an existing sort order with a non-existing one. So there are four cases to cover:

    1. A and B do not exists – treat them equal — 0
    2. A and B exist – sort according to their order-value — calculate position A – B
    3. A exists but not B – A is greater than B — 1
    4. B exists but not A – A is less than B — -1

    Thanks to function support in PHP, we can pass along the order-key and the ordering values into the comparison function easily for a quick example. The function then only has to do what has been outlined for the four cases.

    The example:

    $order    = 'descShort';
    $ordering = ['Regular Cheese', 'No Cheese', 'Regular Sauce', 'No Sauce'];
    $compare  = function($a, $b) use ($order, $ordering) {
        $hasA = array_search($a[$order], $ordering);
        $hasB = array_search($b[$order], $ordering);
    
        // nothing to sort
        if ($hasA === $hasB && $hasA === FALSE) {
            return 0;
        }
    
        // if both are found, sort
        if ($hasA !== FALSE && $hasB !== FALSE) {
            return $hasA - $hasB;
        }
    
        // as one of them is in there, put it to end
        return $hasA === FALSE ? -1 : 1;
    };
    
    usort($array['itemCode'], $compare);
    

    So now there is one caveat: usort is not stable. That means, when returning 0, items do not stay at their position. You can work around that by sorting once again the same with usort.

    usort($array['itemCode'], $compare);
    

    Then the final sort order is (Demo):

    Array
    (
        [itemCode] => Array
            (
                [0] => Array
                    (
                        [code] => P
                        [descShort] => Pepperoni
                    )
    
                [1] => Array
                    (
                        [code] => G
                        [descShort] => Green Peppers
                    )
    
                [2] => Array
                    (
                        [code] => 
                        [descShort] => Regular Cheese
                    )
    
                [3] => Array
                    (
                        [code] => x
                        [descShort] => No Cheese
                    )
    
                [4] => Array
                    (
                        [code] => 
                        [descShort] => Regular Sauce
                    )
    
                [5] => Array
                    (
                        [code] => n
                        [descShort] => No Sauce
                    )
            )
    )
    

    As there was a problem with stable sort and I’m also not that well with it (the suggested function looks a bit like an overhead to me in your case here), there is just good old foreach.

    And as there are no duplicate values for those to be sorted later on, well, this leaves some nifty room:

    $order    = 'descShort';
    $ordering = ['Regular Cheese', 'No Cheese', 'Regular Sauce', 'No Sauce'];
    
    $result = []; // the final result
    $later  = []; // to be sorted later
    foreach($array as $element)
    {
    
        $has = array_search($element[$order], $ordering);
        if ($has !== FALSE) {
            $later[$has] = $element;
            continue;
        }
    
        $result[] = $element;
    }
    

    It is just straight ahead for-eaching over the array, putting all values into $result already that are not part of the ordering.

    Those that are part of the ordering are put into $later with their order value as index already.

    Only $later then is sorted with ksort, and then the two parts are merged:

    ksort($later);
    $result = array_merge($result, $later);
    

    And done. No callback function needed. Just first filtering, and the indexing with the sort value together with ksort does the magic. Demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a view passing on information from a database: def serve_article(request, id): served_article
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.