Empty array

Hello,

Need some help understanding array().

I’m confused about an empty array. How does it work?

I found this example (about shopping carts):

[php]$_SESSION[‘cart’] = array();
$_SESSION[‘cart’][1] = 10;
$_SESSION[‘cart’][6] = 5;[/php]

Again, what does the array() mean? Aren’t you supposed to put values in arrays?

Thanks in advance.

Hello,

The array() function can be used in one of two ways. You can just declare a variable and assign it to the function without parameters: $var = array(); Doing it this way tells the PHP parser that the variable $var will be used as an array. Then later on in your script you can add values to your array by either of these ways:
[php]$var[] = ‘red’;
$var[] = ‘blue’;[/php]

This will add two values to the array from which you can use later:
[php]echo $var[0]; // Echoes red
echo $var[1]; // Echoes blue[/php]

Note that all arrays will start with 0 (zero) as the first key unless you specify the key as follows:
[php]$var[5] = ‘red’;
$var[9] = ‘blue’];[/php]

The example you provided shows you what’s known as a multidimensional array which are arrays with multiple keys. And you don’t have to use numbers as array keys, you can name your keys too. The key in your example is ‘cart’.
[php]$_SESSION[‘cart’] = array(); // Tells the PHP parser to use the already existing array $_SESSION[‘cart’] and make it an array (multidimensional)
$_SESSION[‘cart’][1] = 10; // Assigns the second key in the array (remember arrays start at zero) the value 10
$_SESSION[‘cart’][6] = 5; // Assigns the seventh key in the array the value 5[/php]

Let me know if you need anymore assistance. Hope this helps.

OpzMaster

Thanks so much for the thorough explanation, OpzMaster.

I have just one other question: In your last paragraph, specifically about my example ($_SESSION[‘cart’][1] = 10), since the script assigned a value for the second key, what becomes of the first key (0)? Will php not start with–or “search” for–ZERO (the first key) first? For that matter, why didn’t the script writer start with zero.

I hope I’m understanding it correctly and that my question makes sense.

Thanks again.

Hello again,

In programming, arrays start at zero by default unless you specify the keys to be used. In your example, the programmer started at 1 and skipped zero. PHP will ignore all keys that haven’t been made but will still reference the value in the key that it was assigned. When I program my PHP scripts, I use names for my keys instead of numbers; I only use numbers when I have to. Basically when you don’t specify a key, it will default with numbers:
[php]$var[] = ‘red’; // $var[0]
$var[] = ‘orange’; // $var[1]
$var[] = ‘yellow’; // $var[2]
$var[] = ‘green’; // $var[3]
$var[] = ‘blue’; // $var[4][/php]

Now if you start with a number and use the techniques above, look what happens:
[php]$var[10] = ‘red’; // $var[10]
$var[] = ‘orange’; // $var[11]
$var[] = ‘yellow’; // $var[12]
$var[] = ‘green’; // $var[13]
$var[] = ‘blue’; // $var[14][/php]

The following link will further explain to you about PHP arrays: http://php.net/manual/en/language.types.array.php.

And to answer your last question: I don’t know why the programmer did what he/she did. Since you didn’t provide the entire code, I can’t tell you definitively. Please let me know if you need further assistance.

Cheers!

Thanks again for the wonderfully clear explanation, OpzMaster. Greatly appreciated.

Chris.

For others searching or reading this post…

Arrays in PHP are actually a form of “Linked-Lists”. In the form of “key”=>“Value”.
So, an array can be $array_name[“some_key”]=“some_value”;

An array can use “numeric” keys or a character string such as your [‘cart’] sample. The default is a numeric key starting at “0”.

Another example of an array is when you use SESSION variables. They are just an array called $_SESSION[].
Even when forms are posted, they are send on using a $_POST[] or $_GET[] array.

Arrays are much simpler than they first look.

Thanks for expounding, ErnieAlex.

Sponsor our Newsletter | Privacy Policy | Terms of Service