PHP Arrays Tutorial
Ranked #2,769 in Internet, #162,602 overall
What are arrays?
$array_name = array("sub_variable1" => "value1", "sub_variable2" => "value2", ... );
We can also create a sub-variable to the array, or edit a sub-variable of the array, in another way. It's done the same way as for creating/editing normal variables but this time you don't access $variable_name but a sub variable of an array. So in general: $variable_name['sub_variable_name'] where $variable_name is an array. In the above example we called it $array_name ( as it concerns not just a normal variable but an array ) so we'll do the same in this example:$array_name['sub_variable1'] = "value1";
$array_name['sub_variable2'] = "value2";
Note: If you're new to PHP, I recommend you first check out my PHP Basics Tutorial.
Example of usage
What can arrays be used for, and how?
$shopping_cart = array(9 => 1, 5 => 1, 23 => 4);
This array would then illustrate a shopping cart with the products with the ids 9 (2x), 5 (1x), 23 (4x). If we would want to add another product to the shopping cart we could just do that by ading it to the array, like this:$product_id = 10;
$amount = 1;
$shopping_cart[$product_id] = $amount;
To remove a product from the shopping cart, we'd just unset the sub-variable out of the array:
unset($shopping_cart[$product_id]);
To check whether a certain product is inside of the shopping cart, we'd just use an if loop to check whether the sub-variable $product_id is set inside of the array $shopping_cart:if(isset($shopping_cart[$product_id]) {
}
Foreach loop
Looping through an array.
foreach($array AS $key => $value) {
executable code
}
foreach($shopping_cart AS $id => $amount) {
echo "<p>#{$id} ({$amount}x) </p>";
}
Multi-Dimensional Arrays
$locations = array (
"USA" => array("New York", "Alabama"),
"The Netherlands" => array("Amsterdam", "Utrecht")
);
$locations['USA']; //accesses an array
$locations['USA'][0]; //accesses the first sub-variable of that array (containing "New York")
You can create mult-dimensional arrays at any level. Make them as large and complicated as you like, but it's never good to make your code unecessarily complicated. So try to always keep it as simple as possible (also for the readability).
PHP Functions for Arrays
Useful PHP functions that can be used for arrays.
count($array)
Counts the amount of sub-variables inside of array $array.
Example:
$array = array(10, 4, 21);
$result = count($array); //returns 3
explode($str1, $str2)
Forms an array out of the string $str2, exploding the string by the character/string $str1.
Example:
$str1 = ","; //a comma
$str2 = "keyword1,keyword2,keyword3";
$result = explode($str1, $str2); //creates the array: array("keyword1", "keyword2", "keyword3")
implode($str, $array)
Forms a string out of the array $array, merging all values of the array together to one string using $str1 to merge the values with eachother ( seperate ).
Example:
$str = " and ";
$array = array("item1", "item2", "item3");
$result = implode($str, $array); //creates the string: "item1 and item2 and item3"
strsplit($str1[, $str2])
Converts the string $str into an array with $str2 being the maximum length of the chunk ( if any ).
Example:
$str1 = "abc";
$result = str_split($str1); //creates the array: array("a", "b", "c")
You might also like ...
Related Products (Amazon)
Related Products (eBay)
by webcodez
"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."
webcodez
GameHeroes
more »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- PHP & MySQL Poll System Tutorial PHP & MySQL Poll System Tutorial
- PHP & MySQL Login System Tutorial PHP & MySQL Login System Tutorial
- PHP Smilies System Tutorial PHP Smilies System Tutorial
- PHP & MySQL Visitors Counter Tutorial PHP & MySQL Visitors Counter Tutorial
- PHP Banner Rotator Tutorial PHP Banner Rotator Tutorial
- PHP Email Validation Tutorial PHP Email Validation Tutorial