Array in
PHP
Array is a concept that stores more than one value
of same data type (homogeneous) in single array variable. They are 3 types of
array concepts in PHP.
1. Indexed
Arrays,
2. Associative
Array and
3. Multi-Dimensional
Array.
An array is defined using the keyword “array”. Each
element of line array is assigned on index values which commences from O and
ends with n-1. The user can access the array element using the assname followed
by index value.
Example:
<?php
$teacher_name=array(“Iniyan”, “Kavin”, “Nilani”);
echo “The students name are “ .
$teacher_name[0] . “, “ . $$teacher_name[1] . “ and “ . $teacher_name[2] . “.”;
?>
Associative arrays are a key- value pair data
structure. Instead of storing data in a linear array, with associative arrays
you can store your data in a collection and assign it a unique key which you
may use for referencing your data.
SYNTAX:
Array Syntax:
An Array is defined ().
Associative Arrays Syntax
array(key=>value,key=>value,key=>value,etc.);
key = Specifies the key (numeric or
string)
value = Specifies the value
Example:
<?php
$Marks=array(“Student1”=>“35”,“Student2”=>“17”,“Student3”=>“43”);
echo “Student1 mark is” .
$Marks[‘Student1’] . “ is eligible for qualification”;
echo “Student2 mark is” .
$Marks[‘Student2’] . “ is not eligible for qualification”;
?>
A multidimensional array is an array containing one
or more arrays.PHP understands multidimensional arrays that are two, three, four,
five, or more levels deep. However, arrays more than three levels deep are hard
to manage for most people.
Example:
<?php
// A two-dimensional array
$student=array
(
array(“Iniyan”,100,96),
array(“Kavin”,60,59),
array(“Nilani”,1313,139)
);
echo $$student[0][0].”: Tamil Mark:
“.$student [0][1].”. English mark: “.$student [0] [2].”<br>”;
echo $$student[1][0].”: Tamil Mark:
“.$student [1][1].”. English mark: “.$student [1] [2].”<br>”;
echo $$student[2][0].”: Tamil Mark:
“.$student [2][1].”. English mark: “.$student [2][2].”<br>”;
?>
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.