2. Array An array stores multiple values in one single variable. ..contains value set each element has a value, data stored in the element. And has a key by which the element can be referred to. Department of CE/IT
4. What is an Array? A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable. Department of CE/IT
5. Types of Arrays In PHP, there are three kind of arrays: Numeric array - An array with a numeric index Associative array - An array where each ID key is associated with a value Multidimensional array - An array containing one or more arrays Department of CE/IT
6. Initializing Arrays No of ways to initialize the array. For e.g. $ncststu[] = “Sri”; $ncststu[] = “Raja”; $ncststu[] = “Lakshmi”; $ncststu[123] = “Sri”; $ncststu122] = “Raja”; $ncststu[121] = “Lakshmi”; $ncststu = array (“Sri”, “Raja”, “Lakshmi”); to change the indices of the array use => operator. Department of CE/IT
7. 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: other sollution is to initialise array elements one-by-one as follows: Department of CE/IT $colorList = array("red","green","blue","black","white"); $colorList[0] = "red"; $colorList[1] = "green"; $colorList[2] = "blue"; $colorList[3] = "black"; $colorList[4] = "white";
8. Display the array content If you want only display one element of the array then you can just write the following code: 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: Department of CE/IT echo $colorList[0]; for ($i=0;$i<=4;$i++){ echo $colorList[$i]; }
9. Associative Arrays An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them. Department of CE/IT
10. Example In this example we use an array to assign ages to the different persons: $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); Department of CE/IT
11. Finding the Size of an Array: To find the size of an array, use the function count().count() takes one argument, which is the array, and returns an integer represents the number of the elements of this array. Department of CE/IT