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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:03:44+00:00 2026-06-07T12:03:44+00:00

I can’t see to find an answer for this, but it’s been bugging me

  • 0

I can’t see to find an answer for this, but it’s been bugging me for a while.

When using multidimensional arrays in php (or any language for that matter), do you get any efficiency and productivity out of saving an array of a multidimensional array as its separate array or accessing that “array” element directly from the whole array?

For example,

print_r($myArray);

Array
(
   [2] => Array
      (
         [7.0] => Array
            (
               [0] => 1
               [1] => 23
            )

         [16.0] => Array
            (
               [0] => 4
               [1] => 28
            )
      )
   [5] => Array
      (
         [17.0] => Array
            (
               [0] => 1
               [1] => 3
            )
      )
)   

If I needed to REPEATABLY access the array of [16.0], would it be better to just save that entry as its own array until I don’t need it anymore, or is it better just to access it directly?

Option 1:
$tempArray=$myArray[2]["16.0"];
echo "Index=".$tempArray[0].";

or

Option 2:
echo "Index=".$myArray[2]["16.0"][0];

Of course this is just a tiny example, but if the values in (arbitrary) $array[.][.][n][…] are being accessed more than once, and the value of n depends on the index of a loop, is there any difference between accessing the element directly or saving that array (deep down on the layers of the array) as its own array and accessing its values that way?

  • 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-07T12:03:47+00:00Added an answer on June 7, 2026 at 12:03 pm

    If you look at the bytecode generated for these statements:

    $tempArray=$myArray[2]["16.0"];        
    echo $tempArray[0];        
        FETCH_DIM_R        $3      $myArray, 2        
        FETCH_DIM_R        $4      $3, '16.0'        
        ASSIGN                     $tempArray, $4        
        FETCH_DIM_R        $6      $tempArray, 0        
        ECHO               $6        
    
    echo $myArray[2]["16.0"][0];        
        FETCH_DIM_R        $7      $myArray, 2        
        FETCH_DIM_R        $8      $7, '16.0'        
        FETCH_DIM_R        $9      $8, 0        
        ECHO               $9        
    

    (I’ve reformatted and removed the EXT_STMT markers). You can see that the only difference is the actual assignment to $tempArray. You might think that an array assignment is expensive. However PHP uses a referencing model and does a lazy copy-on-write, so it is not, and the cost is the pretty much same whatever the array size.

    (Except of course if you change elements after the assignemt, so $tempArray is no longer the same as its originating slice, and at that point the memory usage jumps as the assign triggers the clone of the slice as the references need to be split.)

    OK, this approach might be worthwhile if you are doing a lot of localised readonly access to a slice to save the repeated FETCH_DIM_R lookups (the PHP compile does absolutely no local optimisation of repeated use of indexes). However, the opportunities to shoot yourself in the foot on update are significant.

    Why not benchmark this yourself using a loop and microtime() and memory_get_usage()?

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

Sidebar

Related Questions

Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can I load admob ad using AsyncTask ? I have tried it but I
Can I authenticate with just Google account username and password instead of using OAuth?
Can any one tell, how to get the result of LINQ query contains group
Can anyone explain to me why this program: for(float i = -1; i <
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but

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.