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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:52:49+00:00 2026-05-22T23:52:49+00:00

This following piece of code is meant to create a multilevel array, print it,

  • 0

This following piece of code is meant to create a multilevel array, print it, then shuffle it, again print it, and sort the array.

$arr=array(
        array(
            array('a','b','c')
        ),
        array(
            array('d','e','f')
        ),
    array(
            array('g','h','i')
        ),
);
print_r($arr);
shuffle($arr);
print_r($arr);
sort($arr);
print_r($arr);

Now the odd thing that i observed, when shuffle() is used, it only shuffles the indexes of the array that is being shuffled, it doesnt shuffle, the inner most elements a,b,c to something else, but when the the sort() function is used, it sorts back the array into the normal status and the leaf nodes are back to alphabetical order. Why does this happen?

Here is the sample output:
*Original Array*

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => d
                    [1] => e
                    [2] => f
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [0] => g
                    [1] => h
                    [2] => i
                )
        )
)

Shuffled Array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => g
                    [1] => h
                    [2] => i
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [0] => d
                    [1] => e
                    [2] => f
                )
        )
)

Sorted Array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => d
                    [1] => e
                    [2] => f
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [0] => g
                    [1] => h
                    [2] => i
                )
        )
)
  • 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-22T23:52:49+00:00Added an answer on May 22, 2026 at 11:52 pm

    See the array section of PHP’s comparison operator reference, especially the array comparison transcription. Basically PHP first compares the amount of keys in the array, then checks if the arrays have the same keys (in this case, 0 for the inner arrays) and then compares the values. Because you have a nested array there, it proceeds to compare the leaf nodes in sort(), which leads to the array being sorted in this case by the first value of the leaf arrays (a, d and g).

    shuffle() simply reorders the indexes of the array you give it regardless of what the array contains, thus it does not touch the inner arrays at all.

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

Sidebar

Related Questions

this following piece of code works, but it sort of makes the browser quirk
This following code gets this compile error: " invalid types 'int[int]' for array subscript
I have the following piece of code: if((String(parent).length > 0) && (String(this.className).length > 0))
IE what happens if you have this following piece of code? int mean(const vector<int>
I see the following piece of code input[type='button']text:visible:enabled:first What does this code do. Which
i was following an ACL tut. which has used this piece of code. class
I have the following piece of code. QNetworkAccessManager *man = new QNetworkAccessManager(this); QNetworkRequest getRequest;
I found myself writing the following piece of code over and over again: MyModel.find(my_id).my_field
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I'm following this guide: http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html and I'm trying to create my first ruby program.

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.