The main advantage of an Array is instead of having many similar variables, you can store the data as elements in an array. Each element in the array can be accessed easily by its ID.
There are three different kind of arrays:
Numeric array - An array with a numeric ID key
Example: $names = array("John","Mark","Harry");
Associative array - An array where each ID key is associated with a value
$name[0] = "John";
$name[1] = "Harry";
Multidimensional array - An array containing one or more arrays. When storing data about specific named values, rather than a numerical array a multidimensional array will do the best.
$num = array("John"=>20; "Harry"=>30);
Array Functions:
array_diff:
If you want to find out difference of two arrays you can use the function array_diff. This function takes two arrays as input and then returns the result array. Syntax of this function is:
When you need to count the number of time an element is present inside an array you can use array_count_values function. This function returns another array which stores the values the input array and its frequency or the number of occurrence of the value.
The syntax of this function:
array_sum
We can store values in an array and calculate the sum of the values inside the array by using the array_sum function.
array_unique
To get a set of unique values from an array we use this function. This will take an array as input and return an array with unique elements. The duplicate elements will not be repeated.

No comments:
Post a Comment