PHP array
In c like language we all studied that an array is a collection of similar elements. These similar elements could be all int or all floats or all chars . Ber here we prefer to define that array in such a way that are nothing more then lists of information mapped with keys and stored under one variable name.
In PHP the arrays are quite different and even more simple to manage
Array Syntax
php start
$employee=array("pfn"="c567","name"="rahul");
echo $emaployee['name'];
?>
The same can be done by this also
php start
$employee['pfn"]="c567";
$employee["name"]="rahul";
php end
In php each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.
The below is an example of this
php start
$employee= array( array("richa", "mca"),
array("divya", "btech"),
array("rahul", "mca")
);
php end
If we discuss about array and ignore to discuss of array function will bot be fair so below are some basis function of array
--sort($array)
As name suggest it sort array
--rsort($array)
As name suggets it sort array in reverse order
--count($array)
It return total no. of element in an array
Emaple which sort element of array
php start
$color[]="red";
$color[]="green";
$color[]="white";
sort($color);
foreach($color as $c)
echo $c."\n";
php end
This will print array in sorted manner
green
red
white
As you have seen that an array has been iterated by foreach apart from this there are so many ways to iterate it for example for loop while loop.
If require we can print whole array without iteration we can use print_r($array) the below is an example of the same;
php start
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);
php end
out put will
Array
(
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
)
I suggest you to go through the list of array functions for more http://in2.php.net/manual/en/ref.array.php
No comments:
Post a Comment