An array is a group of the same data type which is used to store multiple values in a single variable. In an array each data item is referenced by its name and a number that is called a subscript. The elements of an array are accessed by specifying a subscript after the array variable name. The first element of an array has a subscript of 0, the next is 1, and so on. The last element has a subscript of n-1, where n is the number of elements.
Types of Array
There are three types of arrays in PHP, which are as follows:
Types of Array
There are three types of arrays in PHP, which are as follows:
- Numeric Index Array
- Associative Array
- Multidimensional Array
Numeric Index Array
A numeric index array stores each array element with a numeric index i.e. subscript. In a numeric indexed array, the keys are numeric and starts with subscript of 0, the next is 1, and so on. Numeric indexed arrays use integer/numbers as their index number to identify each item of the array.
Syntax of Numeric Index Array
There are two ways to define the numeric index array in PHP.
Which are as follows:
1. $arrayname =array (“value1″,”value2″,”value3”);
2. $arrayname[0]=”value1″;
$arrayname[1]=”value2″;
$arrayname[2]=”value3″;
Example1
A numeric index array stores each array element with a numeric index i.e. subscript. In a numeric indexed array, the keys are numeric and starts with subscript of 0, the next is 1, and so on. Numeric indexed arrays use integer/numbers as their index number to identify each item of the array.
Syntax of Numeric Index Array
There are two ways to define the numeric index array in PHP.
Which are as follows:
1. $arrayname =array (“value1″,”value2″,”value3”);
2. $arrayname[0]=”value1″;
$arrayname[1]=”value2″;
$arrayname[2]=”value3″;
Example1
Output
Associative Array
An Associative array also is a type of array by which you can assign an arbitrary key to every value. In an associative array, each key is associated with a value. With associative arrays we can use the values as keys and assign values to them. In an associative array, the keys are not necessarily numeric, and even when they are numeric, not necessarily in any order.
Syntax of Associative Array
There are two ways for defining the associative array in PHP. Which are as follows:
1. $arrayname = array (“value”=>20, “value2″<=30, “value3″=>40);
2. $arrayname[‘value1’] = “20”;
$arrayname[‘value2’] = “30”;
$arrayname[‘value3’] = “40”;
Example1
An Associative array also is a type of array by which you can assign an arbitrary key to every value. In an associative array, each key is associated with a value. With associative arrays we can use the values as keys and assign values to them. In an associative array, the keys are not necessarily numeric, and even when they are numeric, not necessarily in any order.
Syntax of Associative Array
There are two ways for defining the associative array in PHP. Which are as follows:
1. $arrayname = array (“value”=>20, “value2″<=30, “value3″=>40);
2. $arrayname[‘value1’] = “20”;
$arrayname[‘value2’] = “30”;
$arrayname[‘value3’] = “40”;
Example1
1
2
3
4
5
6
7
|
<?php
$studentpercent ['Anuj'] = "76";
$studentpercent ['Sanjeev'] = "68";
$studentpercent ['Rahul'] = "90";
echo "This is First Example of Associative Array" .'<br>'.'<br>';
echo "Anuj have " . $studentpercent ['Anuj'] . " Percent marks in B.Tech.";
?>
|
Tidak ada komentar:
Posting Komentar