What is PHP Array
Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. An array is an special variable which can hold more than one value at a time.
So why we use array? we use array for to stores multiple values…like this…
- $laptop_brands = array(“Dell”,“Samsung”,“Asus”,“Lenevo”);
So How to create array? we use array function to create array.
Types Of Array
There are three different types of arrays in PHP:
a) Numeric Array: An array with a numeric ID key.
b) Associative Array: An array where each ID key is associated with a value.
c) Multidimensional Array: An array containing one or more arrays.
a). Numeric Arrays:
Numeric arrays use integer / numbers as their index number to identify each item of the array. The example we discussed above are numeric arrays as they have integer values as index numbers for each item.
<?php
$colours = array(“white”,”black”,”blue”);
print_r($colours);
/*
output will be
Array
(
[0] => white
[1] => black
[2] => blue
)
*/
?>
In the above output you can see the index numbers for white, black and blue are 0,1,2 respectively which are numeric values and hence we call such arrays numeric arrays.
b). Associative Arrays:
Sometimes it’s better to use the index name instead of index number for example if you want to save three students’ names and numbers so your best option will be to use each student’s name as index value for the array and his numbers as the values, behold on the example below,
<?php
$students[‘Jack’] = 90;
$students[‘Jane’] = 60;
$students[‘Hannah’] = 40;
echo “Hannah’s mark in maths is” . $students[‘Hannah’] ;
/* Output is: Hannah’s mark in maths is 40 */
?>
When you submit a form using POST or GET method you get a similar associative array on the receiving page that contains the name of each form field as array index and its value as index value. Try to make a HTML form with some fields and post it and on the receiving page print the global arrays like
print_r($_POST);
print_r($_GET);
and you will see the associative array.
Associative Arrays are more easy to handle and to process information especially dealing with complex form submission and dynamic values from database etc.
c). Multidimensional Arrays:
A multidimensional array can contain arrays within itself and the sub arrays contain more arrays within them.
This is how you can use multidimensional arrays to organize data. Try to submit an array of form fields and then print the global array to check the output, you will get the global array as multidimensional array that will contain more sub arrays.
Please check following example:
<?php
// Define nested array
$contacts = array(
array(
“name” => “Peter Parker”,
“email” => “peterparker@mail.com”,
),
array(
“name” => “Jack Reacher”,
“email” => “jreacher@mail.com”,
),
array(
“name” => “Harry Potter”,
“email” => “harrypotter@mail.com”,
)
);
// Access nested value
echo “Peter Parker’s Email-id is: ” . $contacts[0][“email”];
/*
Output is : Peter Parker’s Email-id is: peterparker@mail.com
*/
?>
