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

  • SEARCH
  • Home
  • 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

Last couple of days i have been trying to make listpreference work on my
I have been reading up and trying Git for the last few days and
During last three days I have been trying to solve Project Euler 15 in
For the last few days, I have been trying to use Python for some
I have been trying there last days to extend the default editor (java, xml,
I have been trying for last 3 days still i am not able to
I have been trying unsuccessfully for the last few days to run Cobertura 2.4
For the last three days I have been trying to develop a web based
I've been trying to get this to work for the last two days but
For the last few days I have been attempting to find a method to

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.