Arrays are special data types. Despite of other normal variables an array can store more than one value.
Creating an array
There are more ways to create an array in PHP. Maybe the most easiest way to create our color list array is the following:
Code:
Code:
$colorList = array("red","green","blue","black","white");
Other sollution is to initialise array elements one-by-one as follows:
Code:
Code:
$colorList[0] = "red";
$colorList[1] = "green";
$colorList[2] = "blue";
$colorList[3] = "black";
$colorList[4] = "white";
If you don't want to bother about numbering - I will explain it later - you can create your array as this:
Code:
Code:
$colorList[] = "red";
$colorList[] = "green";
$colorList[] = "blue";
$colorList[] = "black";
$colorList[] = "white";
Display the array content
If you want only display one element of the array then you can just write the following code:
Code:
Code:
echo $colorList[0];
This code will display the text "red". However you may want to display all elements in the array. You can write a loop and display them like this:
Code:
Code:
for ($i=0;$i<=4;$i++)
{ echo $colorList[$i];}
It is quite easy. However it is not the best sollution as the number of element is hard coded. There is a much better way to display all elements of an array. You can use a foreach loop as this:
Code:
foreach ($colorList as $value)
{ echo $value;}
If you want to display array content for debugging purposes then you can use 2 built in PHP functions. These are the print_r and var_dump. These functions displays tha array as key-value pairs. Besides this var_dump also displays variable informations. You can use them as follows:
Code:
print_r($colorList);echo "";
var_dump($colorList);
Associative arrays
Our colorList array can be defined as associative array like this:
Code:
Code:
$colorList = array("apple"=>"red",
"grass"=>"green",
"sky"=>"blue",
"night"=>"black",
"wall"=>"white");
You have to remember that array keys are case-sensitive, but type insensitive. It means that 'a' differs from 'A' but '1' is the same as 1.
So, the array above can be defined (created) one-by-one elements like this:
Code:
So, the array above can be defined (created) one-by-one elements like this:
Code:
$colorList["apple"] = "red";
$colorList["grass"] = "green";
$colorList["sky"] = "blue";
$colorList["night"] = "black";
$colorList["wall"] = "white";
And you can display the content similar to the normal array, but here you need to use the string value instead of the number.
Code:
Code:
echo "The sky is ".$colorList["sky"] ." and the grass is ".$colorList["grass"];
You can mix your array and use numbers and strings in the same list like this:
Code:
Code:
$colorList["apple"] = "red";
$colorList[5] = "green";
$colorList["sky"] = "blue";
$colorList["night"] = "black";
$colorList[22] = "white";
Multidimensional arrays
As each element value of the array can be any type it means that it can be other array as well. If an array element value is an other array then this is a multidimensional array. Of course the internal array values can be arrays as well and so on. You can define 10 dimensional (or more) array if you want.
Creating a multidimensional array is as almost simple as the normal array. Let's see an example:
Code:
To acces and display an element in the multidimensional array you just extend the key list as follows:
Code:
echo $myLists['cars']['Toyota'];
Array functions
During programming it can be neccessary to manipulate arrays. Do do this PHP has some usefull built in functions.
Get the length of the array, or with other words how many elements are in the array. To get this information you can use the sizeof function. It tells you how big is the actual data. You can use it like this:
Code:
Sometimes you want to remove an element from the array. In this case you can use the unset function like this:
Code:
unset($colorList["sky"]);
To check whether an array has a requested element you can use the isset function as follows:
Code:
And last you sometimes want to sort your array for eaxmple to display its content in alphabetical order. To do this you have more possibilities. There are more built in array sorting functions in PHP. The most known are sort and asort. The difference between them is that sort renumbers the keys so you will lost the key values. So if you need the key names (associative arrays) use the asort function.
You can use them as follows:
Code:
asort($colorList);
Multidimensional arrays
As each element value of the array can be any type it means that it can be other array as well. If an array element value is an other array then this is a multidimensional array. Of course the internal array values can be arrays as well and so on. You can define 10 dimensional (or more) array if you want.
Creating a multidimensional array is as almost simple as the normal array. Let's see an example:
Code:
$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");
To acces and display an element in the multidimensional array you just extend the key list as follows:
Code:
echo $myLists['cars']['Toyota'];
Array functions
During programming it can be neccessary to manipulate arrays. Do do this PHP has some usefull built in functions.
Get the length of the array, or with other words how many elements are in the array. To get this information you can use the sizeof function. It tells you how big is the actual data. You can use it like this:
Code:
echo sizeof($colorList);
Sometimes you want to remove an element from the array. In this case you can use the unset function like this:
Code:
unset($colorList["sky"]);
To check whether an array has a requested element you can use the isset function as follows:
Code:
if (isset($colorList["grass"])) echo "OK";
And last you sometimes want to sort your array for eaxmple to display its content in alphabetical order. To do this you have more possibilities. There are more built in array sorting functions in PHP. The most known are sort and asort. The difference between them is that sort renumbers the keys so you will lost the key values. So if you need the key names (associative arrays) use the asort function.
You can use them as follows:
Code:
asort($colorList);
sort($colorList);
Both of the functions sorts values in ascending order. If you want to sort them descending use the corresponding rsort and arsort functions.
Both of the functions sorts values in ascending order. If you want to sort them descending use the corresponding rsort and arsort functions.
ليست هناك تعليقات:
إرسال تعليق