Wednesday, January 31, 2018

PHP Array tutorials with example

 Arrays

One of the most useful data structures supported by PHP is arrays. An array is a collection of data elements that are of the same type. For instance, if you wish to store 20 integers, then it can be stored in an array of 20 elements. You can also look at arrays as a collection of variables of the same type; each of which is capable of storing data.

PHP supports three types of arrays, which include  :- 

•   numeric arrays, 
•   multi-dimensional arrays  
•   associative arrays. 

The individual elements of the array can be accessed using the index or position of the element in the array. The three types of arrays differ in the manner their elements are accessed. Numeric arrays have numeric index that are incremented in a linear fashion. For instance, the first element of the array has the default index of 0. For all elements succeeding this data value, the index is incremented by 1.

            On the other hand, associative arrays have strings as index. The array is composed in a key-value fashion and the keys are stored as index while the values are accessed using the keys. Therefore, the storage of elements and their accessibility is dependent upon the association between the key and value.

Lastly, an array can be composed of elements each of which is an array in itself. Such an array is 2-dimensional. Correspondingly, the dimensionality of an array can be increased depending on the programming requirement. Arrays that have multiple dimensions are called multi-dimensional arrays. Moreover, the elements of such arrays are accessed using multiple indices.

◆   Numeric Array 

Numeric arrays can store any type of data like strings, numbers and objects. However, the only thing worth remembering is that the array’s indices are numeric. Moreover, the default index of the first element of the array is zero. The function used to create arrays is array(). Sample code to demonstrate the functionality of numeric arrays is given below for your reference.







◆   Associative Arrays 

Associative arrays are similar to numeric arrays in the way they function. However, their indexing mechanism is different. They work on the key-value format. Values have keys, which are The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

For instance, if you need a data structure to store the salaries of employees, then the same can best be done using an associative array. In this case, employee names can be used as keys and salaries as values. Please note that you must never put associative arrays inside double quotes as they don’t return any value in that case. Sample code to demonstrate the functioning of associative arrays is given below –

<html> 

<body> 

<?php 

/*Creating associative array using method 1*/ 

$salary = array('james' => 20000, 'jacob' => 15000, 'maria' => 7500); echo "Salary of first employee is ". 

$salary['james'] . "<br />"; echo "Salary of second employee is ". 

$salary['jacob']. "<br />"; echo "Salary of third employee is ".  

$salary['maria']. "<br />"; 

/*Creating associative array using method 2*/ 

$salary['james'] = "high"; 

$salary['jacob'] = "medium"; $salary['maria'] = "low"; 

echo "Salary of first employee is ". $salary['james'] . "<br />"; 

echo "Salary of second employee is ". $salary['jacob']. "<br />"; 

echo "Salary of third employee is ". $salary['maria']. "<br />"; 

?> 

</body> 

</html> 






◆   Multi-dimensional

Arrays In a multi-dimensional array, every element of the array is also an array. Therefore, a multi-dimensional array is composed arrays, which in turn can also be made up of arrays. In order to access elements of a multi-dimensional array, multiple indices must be used. Sample code to demonstrate the functioning of multi-dimensional arrays is given below –






1 comment:

  1. I LOVE APPLE PRODUCTS
    Great article. Vey good and nice it is vritten.
    Keep up the good work.
    http://bit.ly/ttttttsdf

    ReplyDelete

Please comment and follow this site blog inbox