|
Home | About | Downloads | Features | Demo | FAQ | Forum | Screenshots | PHP Tutorials | Hire a PHP Programmer | |||||||||||||||||||||||||||||||||||||||||||||||||||
<< PHP Switch |
PHP Tutorials |
PHP While Loop >>
What is an Array?Simple. An array is a bunch of data stored in one data, under different labels. Think of it like there are varibles inside of variables. The following HTML table is an example of what an array looks like:
If that confuses you, don't worry. When I said about labels, they are automatically generated by the PHP engine (in most cases). But before I can continue, here is the syntax of an array:
<?php $array = array('Value', 'Value', 'Value', 'Value', 'Value', 'Value', 'Value'); ?> The is how you create an array. The first value will be labeled 0, the second one 1 and so on and so forth. Array values do not have to contain just string data, they can contain any type of data PHP variables can hold. For example, below is a mix between numerical data and string data:
<?php
$array = array('String', 1, 'String', 3, 'String', 5); ?> Using an HTML table, I will show you what that array looks like:
Printing ArraysPrinting arrays is as simple as printing out normal variables. All the extra work is in remembering where your content is in the array. Using the example array above, I'll print out some values:
<?php $array = array('String', 1, 'String', 3, 'String', 5); echo "{$array[0]}<br />"; echo "{$array[1]}<br />"; echo "{$array[2]}<br />"; echo "{$array[3]}<br />"; echo "{$array[4]}<br />"; echo $array[5]; ?> This is what the end users will see:
String
1 String 3 String 5 As you can see the syntax is (key = label): $array[key]
The key is of course the number which corresponds to the value in the array. Not the actual value. Associative ArraysInstead of using the array() language-construct provided by PHP, there is another way. The way I'm going to show you is similar to printing out arrays. You can use this syntax to assign values to an array: $array[key] = value;
That will assign a value to $array[key]. What? You thought there were numbers as keys? Well there are. But there are also ways to use a string value to assign a key. See this example:
<?php $array['food'] = 'carrot'; $array['drink'] = 'water'; $array['php'] = 'easy'; echo "A food? {$array['food']}<br />"; echo "A drink? {$array['drink']}<br />"; echo "PHP? {$array['php']}"; ?> This is what the end users will see:
A food? carrot A drink? water PHP? easy This would be stored in the array like this:
See? No numbers needed at all. This makes it a lot easier to remember where you data is kept. You can also set associative arrays using the array() language-construct provided by PHP. The syntax is: $array = array(key => value, key => value);
Here is an example using that method:
<?php
$array = array('food' => 'carrot', 'drink' => 'water', 'php' => 'easy'); echo "A food? {$array['food']}<br />"; echo "A drink? {$array['drink']}<br />"; echo "PHP? {$array['php']}"; ?> The end users will see exactly the same thing as the previous example. << PHP Switch | PHP Tutorials | PHP While Loop >> |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| © Douglas Rennehan 2007 - 2010 | Terms of Service | Privacy Policy | Valid XHTML 1.0 | Valid CSS | ||||||||||||||||||||||||||||||||||||||||||||||||||||