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 972399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:07:05+00:00 2026-05-16T03:07:05+00:00

For the last 3 days, I have been trying to sort an array, but

  • 0

For the last 3 days, I have been trying to sort an array, but without success.

I tried in the php file first and in the tpl file, but it was impossible for me to sort my array.

Can you help me please ??

This is the structure of my array (thanks to the smaty debug tool !) :

Array (5)
attributes => Array (4)
  23 => "1L"
  24 => "3.5L"
  21 => "50ml"
  22 => "350ml"
name => "Contenance"
is_color_group => "0"
attributes_quantity => Array (4)
  23 => 1
  24 => 500
  22 => 500
  21 => 500
default => 21

I wish to sort it by the ascending “id” to obtain this kind of result :

Array (5)
attributes => Array (4)
  21 => "50ml"
  22 => "350ml"
  23 => "1L"
  24 => "3.5L" 
name => "Contenance"
is_color_group => "0"
attributes_quantity => Array (4)
  21 => 500
  22 => 500
  23 => 1
  24 => 500  
default => 21

Have you an idea ?

  • 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-05-16T03:07:06+00:00Added an answer on May 16, 2026 at 3:07 am

    Use uksort:

    uksort( $your_array['attributes'], 'my_sort_func' );
    uksort( $your_array['attributes_quantity'], 'my_sort_func' );
    
    function my_sort_func( $a, $b )
    {
        if( $a == $b )
            return 0;
    
        return ($a < $b) ? -1 : 1;
    }
    

    As zerkms noted, there no need to use uksort as you only need a basic numeric comparison. This is achieved using simply ksort():

    ksort( $your_array['attributes'] );
    ksort( $your_array['attributes_quantity'] );
    

    Use uksort() when your keys cannot be sorted by its numerical value. For example, strings.

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

Sidebar

Related Questions

Over the last few days I have noted a few web sites that demonstrated
I've tried figuring out this problem for the last 2 days with no luck.
We got strange error last days. ___doPostBack is undefined. We are building quite advanced
I'm using MDaemon as out mail server and the last days I get an
thanks for all the help the last few days. I'm hoping someone knows this
I'm playing with ASP.NET MVC for the last few days and was able to
I'm currently using the following SQL for retrieving the last seven days worth of
Last couple of days we were discussing at another question the best to manage
Given a week-day (1-7), how can I calculate what that week-day's last date was?
Last year, Scott Guthrie stated You can actually override the raw SQL that LINQ

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.