Home | Store | osCommerce Tutorials

PHP Array

The PHP files of osCommerce site uses a lot of arrays. Therefore this is better to understand array well, otherwise you will get lost later when studying the code structures of osCommerce shop.

In brief, array is used to store data in an orderly manner. Actually you may consider an array as a pigeon hole shelf, and each pigeon hole is used to store your belongings

osCommerce array

Piegon hole shelves may have only one column, two columns or even more columns. PHP arrays are also the same. Arrays may have:

  • one column (one-dimensional array), or
  • two columns (two-dimensional array), or
  • even have more columns (multi-dimensional array).

The most commonly used arrays are one-dimensional array and two-dimensional array.

PHP arrays are a bit difficult to understand than variables. However this is fun and powerful. I think you prefer to buy a pigeon hole shelf (array) rather than buying a lot of single cell shelf (variable) stacking up to-gether.

One-dimensional PHP Array

A one-dimensional array is like a pigeon hole shelf with a single column.

osCommerce array

Let's put something in the pigeon hole shelf.

osCommerce array

Numerical Indexed Array

We can create (store) the above things (data) in an array. There are some methods to create an array (store the above things):

Method 1 (Easy to Understand):

<?php

// Create an array to store things (data)
// Assign the things (data) one by one with a key

$shelf [0] = "book";
$shelf [1] = "shoe";
$shelf [2] = "flower";

echo "<h2>Shelf Storage</h2>";

// Show what is storing on shelf
echo $shelf[0] . "<br />";
echo $shelf[1] . "<br />";
echo $shelf[2] . "<br />";

?>

As you can see, it simply assign a key of an array equal to a value. This can be illustrated in the diagram below:

PHP Array structure

Note:
The key of an array starts with zero NOT one.

Method 2 (Similar to Method 1):

<?php

// Create an array to store things (data)
// Assign the things (data) without key
// The key will be assigned automatically start from zero

$shelf [] = "book";
$shelf [] = "shoe";
$shelf [] = "flower";

echo "<h2>Shelf Storage</h2>";

// Show what is storing on shelf
echo $shelf[0] . "<br />";
echo $shelf[1] . "<br />";
echo $shelf[2] . "<br />";

?>

This method is same as the above example except that the key of array is left blank. In this situation, a key will be assigned automatically, starting from one.

Method 3 (Most Commonly Used Method):

<?php

// Create an array to store things (data)
$shelf = array ("book",
"shoe",
"flower"
);

echo "<h2>Shelf Storage</h2>";

// Show what is storing on shelf
echo $shelf[0] . "<br />";
echo $shelf[1] . "<br />";
echo $shelf[2] . "<br />";

?>

In both methods, the output of the above array examples should look like:

Shelf Storage

book
shoe
flower

In this example, we learned that each value (hole) of the array (shelf) has an index. Therefore we can get the data with reference to the index.

osCommerce array

Therefore:

$shelf[0] is storing the "book"

$shelf[1] is storing the "shoe"

$shelf[2] is storing the "flower"

Associative Arrays

Sometimes, we like to use a meaningful index rather than using number. Actually you can use name for the index.

osCommerce array

We store the above things (data) in an array like that:

<?php

// Create an array to store things (data)
$shelf = array ( "top hole" => "book",
"middle hole" => "shoe",
"bottom hole" => "flower"
);

echo "<h2>Shelf Storage with Name</h2>";

// Show what is storing on shelf
echo $shelf["top hole"] . "<br />";
echo $shelf["middle hole"] . "<br />";
echo $shelf["bottom hole"] . "<br />";

?>

The output of the above array example should look like:

Shelf Storage with Name

book
shoe
flower

Two-dimensional PHP Array

Now we have the most basic concept of array. We can go a bit further.

A two-dimensional array is similar to a pigeon hole shelf with two columns.

osCommerce array

Imagine that the two-column pigeon shelf is now storing things like that:

osCommerce array

If we use array to store the things (data), the codes will be like that:

<?php

// Create an two-dimensional array to store data
$shelf = array (
array("one book" , "three books"),
array("one shoe" , "two shoes"),
array("one flower" , "one flower")
);

echo "<h2>Two Column Shelf Storage</h2>";

// Shows what is storing on shelf
echo 'The top layer is storing: ' .       $shelf[0][0] . " and " . $shelf[0][1] . "<br />";
echo 'The middle layer is storing: ' .  $shelf[1][0] . " and " . $shelf[1][1] . "<br />";
echo 'The bottom layer is storing: ' . $shelf[2][0] . " and " . $shelf[2][1] . "<br />";

?>

The output of the above PHP array example should look like:

Two Column Shelf Storage

The top layer is storing: one book and three books
The middle layer is storing: one shoe and two shoes
The bottom layer is storing: one flower and one flower

As you can see that the above $shelf array contains three arrays. You can use the following codes to print out the structure of the $shelf array:

<?php

// Create an two-dimensional array to store data
$shelf = array (
array("one book" , "three books"),
array("one shoe" , "two shoes"),
array("one flower" , "one flower")
);

echo '<pre>';
print_r($shelf);
echo '</pre>';

?>

The output of this example will be:

Array
(
[0] => Array
(
[0] => one book
[1] => three books
)

[1] => Array
(
[0] => one shoe
[1] => two shoes
)

[2] => Array
(
[0] => one flower
[1] => one flower
)

)

The following diagram illustrate the structure of a two-dimensional array:

PHP two dimensional array

In other words, each element of a PHP two dimensional array need to be referred by:

  • a row index,
  • and a column index.

osCommerce array

If the array is very large (for example, 10 rows x 10 columns), we need to use a loop statement to loop through the array. The following example shows how to use a For Loop to display all elements of an array.

<?php

// Create an two-dimensional array to store data
$shelf = array (
array("one book" , "three books"),
array("one shoe" , "two shoes"),
array("one flower" , "one flower")
);

echo "<h2>Two Column Shelf Storage</h2>";

// Shows what is storing on shelf with a For Loop
for ($row = 0; $row < 3; $row++)
{
echo "<b>The shelf layer $row contains: </b>";

for ($col = 0; $col < 2; $col++)
{
echo $shelf [$row][$col] . " ";
}

echo "<br />";

}

?>

The output of the above PHP array example will be:

Two Column Shelf Storage

The shelf layer 0 contains: one book three books
The shelf layer 1 contains: one shoe two shoes
The shelf layer 2 contains: one flower one flower

This is the end of PHP array tutorial for osCommerce site.