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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:05:13+00:00 2026-06-07T04:05:13+00:00

I have this multidimensional array: $data[] = array(‘name’ => ‘Mini 16’, ‘id’ => 105);

  • 0

I have this multidimensional array:

$data[] = array('name' => 'Mini 16', 'id' => 105);
$data[] = array('name' => 'Mini 15', 'id' => 5650);
$data[] = array('name' => 'Mini 100', 'id' => 9889);
$data[] = array('name' => 'Mini 20', 'id' => 587);

I want to order the array by name column sorting naturally, but is difficult for me.

The expected result:

[
    ['name' => 'Mini 15', 'id' => 5650],
    ['name' => 'Mini 16', 'id' => 105],
    ['name' => 'Mini 20', 'id' => 587],
    ['name' => 'Mini 100', 'id' => 9889]
]
  • 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-07T04:05:16+00:00Added an answer on June 7, 2026 at 4:05 am

    You can use usort() to sort the array by a custom function, and use strnatcmp() to do the natural comparison of two strings like so:

    usort( $data, function( $el1, $el2) { return strnatcmp( $el1['name'], $el2['name']); });
    

    So before, your array was this:

    array(4) {
      [0]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 16"
        ["id"]=>
        int(105)
      }
      [1]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 15"
        ["id"]=>
        int(5650)
      }
      [2]=>
      array(2) {
        ["name"]=>
        string(8) "Mini 100"
        ["id"]=>
        int(9889)
      }
      [3]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 20"
        ["id"]=>
        int(587)
      }
    }
    

    And now it looks like:

    array(4) {
      [0]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 15"
        ["id"]=>
        int(5650)
      }
      [1]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 16"
        ["id"]=>
        int(105)
      }
      [2]=>
      array(2) {
        ["name"]=>
        string(7) "Mini 20"
        ["id"]=>
        int(587)
      }
      [3]=>
      array(2) {
        ["name"]=>
        string(8) "Mini 100"
        ["id"]=>
        int(9889)
      }
    }
    

    Note that for lower versions of PHP, you won’t be able to use an anonymous function, and would instead need something like this:

    usort( $data, create_function( '$el1, $el2', 'return strnatcmp( $el1[\'name\'], $el2[\'name\']);' ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this example: var name; var id; var array = []; $.each(data, function(index,
I have a multidimensional array converted to JSON data in this format. [[null,null,null,null,null,null],[null,null,null,1,1,null],[null,null,null,null,1,1],[null,null,null,null,null,null],[null,null,null,null,null,null]] I
I have a multidimensional array with various config settings. Here's an example: $this->data =
I have CSV data loaded into a multidimensional array. In this way each row
I have an multidimensional array that looks like this: Array ( [0] => Array
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
I have a multidimensional array that looks something like this: ourThing = array( 'id'
How do I form a multidimensional array using MySQL data? I'm using this code
Quick Question: I have the following array (multidimensional): Array ( [preorder] => 0 [data]
I have a multidimensional array with the following structure: teamMembers[teamMembers][0][Name]; teamMembers[teamMembers][0][Email]; teamMembers[teamMembers][1][Name]; teamMembers[teamMembers][1][Email]; etc.

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.