BCA 4 45 PHP Programming
BCA 4 45 PHP Programming
UNIT-V
Array: Anatomy of an Array ,Creating index based and Associative array ,Accessing array, Looping with Index
based array, with associative array using each() and foreach(), Some useful Library function.
Simple arrays
Arrays are a special type of variable that can contain many variables, and hold them in a list.
For example, let's say we want to create a list of all the odd numbers between 1 and 10. Once we create the list, we
can assign new variables that will refer to a variable in the array, using the index of the variable.
To use the first variable in the list (in this case the number 1), we will need to give the first index, which is 0, since
PHP uses zero based indices, like almost all programming languages today.
$odd_numbers = [1,3,5,7,9];
$first_odd_number = $odd_numbers[0];
$second_odd_number = $odd_numbers[1];
Useful functions
The count function returns the number of members an array has.
$odd_numbers = [1,3,5,7,9];
echo count($odd_numbers);
Execute Code5-
The reset function gets the first member of the array. (It also resets the internal iteration pointer).
$odd_numbers = [1,3,5,7,9];
$first_item = reset($odd_numbers);
echo $first_item;
use the index syntax to get the first member of the array, as follows:
$odd_numbers = [1,3,5,7,9];
$first_item = $odd_numbers[0];
echo $first_item;
Execute Code
The end function gets the last member of the array.
$odd_numbers = [1,3,5,7,9];
$last_item = end($odd_numbers);
echo $last_item;
Execute Code
We can also use the count function to get the number of elements in the list, and then use it to refer to the last
variable in the array. Note that we subtract one from the last index because indices are zero based in PHP, so we
need to fix the fact that we don't count variable number zero.
$odd_numbers = [1,3,5,7,9];
$last_index = count($odd_numbers) - 1;
$last_item = $odd_numbers[$last_index];
echo $last_item;
Execute Code
Concatenating arrays
We can use the array_merge to concatenate between two arrays:
$odd_numbers = [1,3,5,7,9];
$even_numbers = [2,4,6,8,10];
$all_numbers = array_merge($odd_numbers, $even_numbers);
print_r($all_numbers);
Execute Code
Sorting arrays
We can use the sort function to sort arrays. The rsort function sorts arrays in reverse. Notice that sorting is done
on the input array and does not return a new array.
$numbers = [4,2,3,1,5];
sort($numbers);
print_r($numbers);
Execute Code
Output:
Season are: summer, winter, spring and autumn
Output:
Size is: Big
Size is: Medium
Size is: Short
Count Length of PHP Indexed Array
PHP provides count() function which returns length of an array.
1. <?php
2. $size=array("Big","Medium","Short");
3. echo count($size);
4. ?>
Output:
3
PHP Associative Array
PHP allows you to associate name/label with each array elements in PHP using => symbol. Such way, you can
easily remember the element because each element is represented by label than an incremented number.
Definition
There are two ways to define associative array:
1st way:
1. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
2nd way:
1. $salary["Sonoo"]="550000";
2. $salary["Vimal"]="250000";
3. $salary["Ratan"]="200000";
Example
File: arrayassociative1.php
1. <?php
2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
3. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
4. echo "Vimal salary: ".$salary["Vimal"]."<br/>";
5. echo "Ratan salary: ".$salary["Ratan"]."<br/>";
6. ?>
Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000
File: arrayassociative2.php
1. <?php
2. $salary["Sonoo"]="550000";
3. $salary["Vimal"]="250000";
4. $salary["Ratan"]="200000";
5. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
6. echo "Vimal salary: ".$salary["Vimal"]."<br/>";
7. echo "Ratan salary: ".$salary["Ratan"]."<br/>";
8. ?>
Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000
Traversing PHP Associative Array
By the help of PHP for each loop, we can easily traverse the elements of PHP associative array.
1. <?php
2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
3. foreach($salary as $k => $v) {
4. echo "Key: ".$k." Value: ".$v."<br/>";
5. }
6. ?>
Output:
Key: Sonoo Value: 550000
Key: Vimal Value: 250000
Key: Ratan Value: 200000
PHP Multidimensional Array
PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP
multidimensional array can be represented in the form of matrix which is represented by row * column.
Definition
1. $emp = array
2. (
3. array(1,"sonoo",400000),
4. array(2,"john",500000),
5. array(3,"rahul",300000)
6. );