Jump to content

multidimensional array faster than variable


Cardale

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.