Monday, April 30, 2012
Arrays in PHP
Monday, April 30, 2012 by v

PHP is growing as widely used language for dynamic websites. Many top websites use PHP for their web pages. Reason is its flexibility and reliability. PHP provides many inbuild functions which simply worth the effort. You do not need to write much of code by your own. Well, data types, loops, conditions, variables are of just one touch course which any person with slight knowledge in C can learn. But most often it is seen that this does not use to be the same case when it comes to arrays. Arrays are somewhat complex and need little effort to understand. Although, its not a big deal in PHP to work out with arrays but still I thought of writing this post in case someone still searching for the answer.
Types of Arrays in PHP
Type 1: Numerical Arrays
Type 2: Associative Arrays
Type 2: Multidimensional Arrays
Numerical Arrays
This type of arrays are easier to use and are most often implemented when there is long list of elements in an Array. These type of arrays are created using following set of code:
$cars=array("Saab","Volvo","BMW","Toyota");
In above example, index is assigned automatically to each element starting from zero (0). If we want to create array manually, then we can write above set of code as:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
To print this list of arrays, we can write following code in PHPfor($i=1;$i<=(sizeof($cars));$i++)
{
echo $cars[$i];
}
Associative Arrays
In associative arrays, unlike numerical arrays, rather than index, a unique value is assigned to each array element. Below is sample code to create associative arrays:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
In other form, we can write above code as:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
To print value of any of the above array, we can simply use following echo statement or you can use any other of your own.
echo "Peter is " . $ages['Peter'] . " years old.";
Multidimensional Arrays
Each element in main array can also be an array and moreover, each sub array can also be array. Such type of arrays are called multidimensional arrays.
Below is the sample code to create multidimensional arrays with automatically generated ID's.
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
Numeric Arrays Vs Associative Arrays
Numeric arrays are useful wherever data to be handle in arrays is in large amount. However, you can not assign ID's to each array element individually. So, it is the only choice to opt for.
On the other hand, when you feel the need of user interface in the code, you must use associative arrays. It is useful from developers point of view. By seeing arrays elements developer can guess what its about and from its corresponding ID, he/she can know the value of that particular element.
Subscribe to:
Post Comments (Atom)
0 Responses to “Arrays in PHP”
Post a Comment