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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:15:17+00:00 2026-05-31T13:15:17+00:00

I have this array structure Array ( [xyz] => Array ( [1] => 3

  • 0

I have this array structure

Array
(
    [xyz] => Array
        (
            [1] => 3
            [0] => s
        )

    [dds] => Array
        (
            [a] => 96
            [d] => 4

         )
...
)

How do I sort on the first dimension so that dds would be first. ksort does not work (at least for me).

TIA

edit
Here is my simple code. I read a serialized 2D array file into a variable, and do a sort on it, nothing complicated, ksort works on my 1D array without a problem. If I comment out the ksort line, then it runs without errors.

$inputfile = "xxx/result.txt";    
$builta = file_get_contents($inputfile);
$built =unserialize($builta);
    $size = sizeof($built);
    echo $size;

    echo "<br><pre>";
    print_r ($built);
    echo "</pre>";

    ksort($built);

    echo "<br><pre>";
    print_r ($built);
    echo "</pre>";

Nothing complicated.

Update: after encountering more errors from dealing with large arrays, it appeared that the problem with ksort was due to not having enough memory to sort large arrays. The solution offered by tpaksu is a great workaround.

  • 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-31T13:15:18+00:00Added an answer on May 31, 2026 at 1:15 pm

    I assume your array’s name is $my_array; then you can use something like this:

    <?php
    $my_array = array(z => array(p=>1,gf=>"d"),v=>array(l=>"m",fr=>"3"));
    var_dump($my_array);
    
    $sorted_keys = array_keys($my_array);
    sort($sorted_keys,SORT_STRING);
    $new_array = array();
    
    var_dump($sorted_keys);
    
    foreach($sorted_keys as $key){
        $new_array[$key] = $my_array[$key];
    }
    
    var_dump($new_array);
    
    ?>
    

    But yes ksort would be better and simpler.

    Edit : Sorry my bad. using sort as inline function was a mistake. Here’s the updated,tested and working code.

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

Sidebar

Related Questions

I have an array structure that looks like this: Array ( [0] => Array
I have two arrays the first one has this structure Parameter Keys array array
I have an array with this structure: array(4) { [0]=> array(x) { [0]=> string(x)London
Lets say I have an array with a structure like this: $arr= Array( array(
i have this json structure and I made it into an array. I was
I have an array structure like this : Array ( [donate] => Array (
I have an array called $all_countries following this structure: Array ( [0] => Array
I have this array structure, it is stored in a variable $xxx Array (
I have this array structure: Array ( [0] => Array ( [0] => Array
I have the following array with a structure within each array (see this jsfiddle

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.