Cardale Posted November 30, 2009 Share Posted November 30, 2009 I am going through some information and wanted to know if a multidimensional array is faster than simply creating a variable and reassigning it with whatever added value? I guess my understanding of this multidimensional array would be like two tables one with a pointer value that adds information together to form the real value. Is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/ Share on other sites More sharing options...
mikesta707 Posted November 30, 2009 Share Posted November 30, 2009 I think your idea of arrays in general is kind of skewed. In PHP multidimensional arrays are nothing more than arrays of arrays. As far as if one is faster than the other, neither is really much faster as far as storing information goes. Traversing through an array is obviously going to be slower than accessing the value of a variable... but speed isn't really the point. Arrays are used to group related information. single variables are for any information that can't or shouldn't be grouped with other information Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968300 Share on other sites More sharing options...
Cardale Posted November 30, 2009 Author Share Posted November 30, 2009 In an array I am missing the point then because it seems that $value = $oldvalue . $newvalue; or $value['old']['new']; Is the same thing? Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968307 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 In an array I am missing the point then because it seems that $value = $oldvalue . $newvalue; or $value['old']['new']; Is the same thing? In the first one you are just concatenating two values into $value, the other one you are accessing a single value inside a multi-dimensional array. As far as speed goes, arrays will be quicker. I do not know the technical parts behind it, but I do know that you can access data in an array element faster. It is probably something to do with an array has a pre determined size (not sure in PHP but I know in like VB/C++ etc you have to pre-define the array size). So I am sure PHP buffers this size and allows for additional items to be added. Where as a variable being created creates a new spot in memory. EDIT: Well doing some reading it looks like I was wrong in my head. Anyhow there are times to use an array vs variables and using them properly will give your script the best performance. Like I said I do not understand the technical part of it (I never really liked all that mumbo jumbo although I should have learned it better). But an array will access data quicker than creating a new variable. However, as mike stated, you should use the proper item for what you need, if you need to increment a variable then assigning a variable to a number is better vs holding a collection of data such as database connection info. But $value = $oldvalue . $newvalue; is not the same as $value['old']['new'] Example is here: <?php $oldvalue = "test "; $newvalue = " test 2"; $value = $oldvalue . $newvalue; echo "The new value of value is: {$value}<br />"; $value = array("old" => array("new" => array("test"))); echo "The value stored in array at index of old and index of new inside old is: {$value['old']['new']}"; ?> Hopefully that clears it up a bit more. Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968336 Share on other sites More sharing options...
Cardale Posted November 30, 2009 Author Share Posted November 30, 2009 Is there any example of a realistic real life approch to this? Something that is useful to give me an idea of how this would be applied? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968359 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 Is there any example of a realistic real life approch to this? Can you provide example data that you want to go through and what you would like done with it? Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968374 Share on other sites More sharing options...
Cardale Posted November 30, 2009 Author Share Posted November 30, 2009 Oh I'm sorry I don't have any exact code I am referring to. I am simply trying to better understand the concepts. Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968395 Share on other sites More sharing options...
Cardale Posted November 30, 2009 Author Share Posted November 30, 2009 I guess I can't come up with an example because I don't understand why anyone would use a multidimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968432 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 Ok, here is an example: Say on my website I want categories with sub-categories and I want them in a multi-dimm array so I can display them with a simple loop so the array would be setup something like this: <?php $categories = array("Top Level1" => array("Sub Level1-1", "Sub Level1-2"), "Top Level2" => array("Sub Level 2-1"), "Top Level3"); foreach ($categories as $category => $subCat) { if (is_array($subCat)) { echo $category . "<br />"; foreach ($subCat as $sub) { echo " " . $sub . "<br />"; } }else { echo $subCat . "<br />"; } echo "<br />"; } ?> The above is a very rough and simple example, hopefully it helps you understand what you may want to use a multi-dimm array for. Quote Link to comment https://forums.phpfreaks.com/topic/183442-multidimensional-array-faster-than-variable/#findComment-968440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.