Politician uddhav thackeray biography- Full Details
Array
1. Array
Array allow you to store groups of related data
in one variable.
For example, if you have a list of items, which
are to be stored in single variables, looks like:
$Company1=“Reliance”;
$Company2= “Tata”;
$Company3= “Birla”;
2. Each element in the array has its own index so
that it can be easily accessed.
There are three kinds of arrays in PHP:
• Numeric array
• Associative array
• Multidimensional array
3. Numeric Array
Numeric array can store numbers, strings and
any object but their index will be represented by
numbers.
By default array index starts from 0.
Example:
There are two methods of creating a numeric
array
5. Associative Arrays
Associative arrays are very similar to numeric
arrays in term of functionality but they are
different in terms of their index.
Associative array will have their index as string
so that you can establish a strong association
between key and values.
6. Example
$salaries = array( “john" => 2000, “qadir" =>
1000, "zara" => 500 );
echo “salary of john is”. $salaries[‘john’];
echo “salary of qadir is”. $salaries[‘qadir’];
echo “salary of zara is”. $salaries[‘zara’];
7. $salaries[‘john'] = 2000;
$salaries['qadir'] = 1000;
$salaries['zara'] = 500;
echo “salary of john is”. $salaries[‘john’];
echo “salary of qadir is”. $salaries[‘qadir’];
echo “salary of zara is”. $salaries[‘zara’];
12. count the values :
echo count($array1);
Max value
echo max($array1);
Min value
echo min($array1);
Sum of elements
echo array_sum($array1);
Sort:
sort($array1); print_r($array1);
Reverse Sort:
rsort($array1); print_r($array1);
In Array:
in_array(13, $array1);
13. Foreach loop
foreach($array as $value)
{
statement;
}
Example:
$values = array(8,19,24,57);
foreach($values as $value)
{
echo “value is = “ . $value;
}
Output:
value is = 8
value is = 19
value is = 24
value is = 57