To day We will look @ arrays in php
in PHP we can find two types of arrays that one is array that we usually used
with numeric indexed, and another one is associative arrays. this type of arrays
also behave as same as usuall arays we used to.. only difference is we can use alphanumeric key index.
You can understand this with following example..
normally array is key value paired .
ex1:
[0] => 456
[1] => 3456
[2] => 566
[3] => 70
-------------------------------------------
ex 2:
["name"] => "Mc.Donald"
["ID"] => 1234567889
["SSN"] => "239898K"
["Country"] => "Australia"
["age"] => 45
in first example array index keys are numeric values {0,1,2,3} but in example 2 it is an alphanumeric values {name,ID,SSN,Country,age}
How can create and access the arrays in PHP
arrays can be created and accessed in normall way how we do in other languages only difference is we dont specify the type of the values that holds by the array
see below example how we create array.
$Marks= array(0=>12,1=>34,2=>50,3=>60,4=>45); this array definition is also same as below definition..
$MarksII= array(12,34,50,60,45); // with this style index is autogenerated.
echo $Marks[0]; // output is 12
echo $MarksII[0]; // output is 12
now we see ho we can create an associative array ..
this is also same as normal array definition.
$StudentsMarks = array("kamal"=>30,"amal"=>34,"nimal"=>50,"sunil"=>20); // this holds an marks of each stdents for a particular subject
how we can print marks of a given student.
echo $StudentsMarks["amal"]; /// this wil echoes the marks of student amal. output is 34
echo $StudentsMarks["sunil"]; /// this wil echoes the marks of student sunil. output is 20
when we working with array we have to loop through the array. how we can loop associative array
is like this...
foreach($key=key($StudentsMarks))
{
echo "marks of $key : $StudentsMarks[$key]<>";
}
this will prints an out put like below...
marks of kamal : 30
marks of amal : 34
marks of nimal : 50
marks of sunil :20
i think you got some idea about arrays in PHP and im retain @ here.. we'll meet again with onother lesson
thank you..
No comments:
Post a Comment