$array=array(); // One D
$array=array(array()); // Two D
for One D
$colorList = array("red","green","blue","black","white");
$colorList[0] = "red";
$colorList[1] = "green";
$colorList[2] = "blue";
$colorList[3] = "black";
$colorList[4] = "white";
If you don't want to bother about numbering, you can create your array as this:
Creating array without numbering
$colorList[] = "red";
$colorList[] = "green";
$colorList[] = "blue";
$colorList[] = "black";
$colorList[] = "white";
Code to display all the elements in the array
for ($i=0;$i<count($colorList);$i++){
echo $colorList[$i];
}
foreach ($colorList as $value) {
echo $value;
}
Associative arrays
Defining an associated array
$colorList = array("apple"=>"red",
"grass"=>"green",
"sky"=>"blue",
"night"=>"black",
"wall"=>"white");
Creating the Multidimensional array
$myLists['colors'] = array("apple"=>"red",
"grass"=>"green",
"sky"=>"blue",
"night"=>"black",
"wall"=>"white");
$myLists['cars'] = array("BMW"=>"M6",
"Mercedes"=>"E 270 CDI",
"Lexus"=>"IS 220d",
"Mazda"=>"6",
"Toyota"=>"Avensis");
Accessing the multidimensional array
echo $myLists['cars']['Toyota'];
Two dimensional array:
$array = array(
array(0, 1, 2),
array(3, 4, 5),
);
or
$array = array();
$array[] = array(0, 1, 2);
$array[] = array(3, 4, 5);
No comments:
Post a Comment