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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:40:38+00:00 2026-05-26T19:40:38+00:00

I have a 2D array that looks like this: array(2) { [45]=> array(5) {

  • 0

I have a 2D array that looks like this:

array(2) {
  [45]=>
  array(5) {
    [0]=>
    int(2)
    [1]=>
    int(5)
    [2]=>
    int(1)
    [3]=>
    int(3)
    [4]=>
    int(4)
  }
  [42]=>
  array(5) {
    [0]=>
    int(5)
    [1]=>
    int(4)
    [2]=>
    int(3)
    [3]=>
    int(2)
    [4]=>
    int(1)
  }
}

The key values of the outer array are numerical, but do not start at 0, and are not sequential. I want to sort the outer array by ascending keys, and the inner arrays by ascending values, so I try this:

ksort($arr);
foreach ($arr as $a) {
    sort($a);
}
var_dump($arr);

Which sorts the outer array as expected, but doesn’t seem to touch the inner arrays at all:

array(2) {
  [42]=>
  array(5) {
    [0]=>
    int(5)
    [1]=>
    int(4)
    [2]=>
    int(3)
    [3]=>
    int(2)
    [4]=>
    int(1)
  }
  [45]=>
  array(5) {
    [0]=>
    int(2)
    [1]=>
    int(5)
    [2]=>
    int(1)
    [3]=>
    int(3)
    [4]=>
    int(4)
  }
}

Why is this, and how can I achieve what I want? I think it’s something to do with the array being nested, because the following works as expected:

$test = array(5,2,3,1,4);
sort($test);
var_dump($test);

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}
  • 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-26T19:40:38+00:00Added an answer on May 26, 2026 at 7:40 pm

    foreach iterates over a copy of the array. If you want to modify the actual values, you have to reference them:

    //               v
    foreach ($arr as &$a) {
        sort($a);
    }
    unset($a);
    

    From the documentation:

    As of PHP 5, you can easily modify array’s elements by preceding $value with &. This will assign reference instead of copying the value.

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

Sidebar

Related Questions

I have a session that looks like this: array(3) { [counter]=> int(0) [currentItem]=> string(1)
I have an array that looks like this: struct table_elt { int id; char
I have a array that looks like this Array ( [provider] => Array (
I have an array that looks like this: Array ( [0] => Array (
I have an array that looks like this, Array ( [0] => 849710414 [1]
I have an Array that looks like this: array( 0 => array( 'key1' =>
Hi I have an array that looks like this : Array ( [0] =>
In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
Say, I have an array that looks like this: var playlist = [ {artist:Herbie
I have a 2d array that looks like this: XX xx What's the most

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.