Types of array in php with example
An array is a sort of data structure used to hold values of one or more related types together. For instance, it is simple to build an array of 100 length if you wish to store 100 values rather than creating a 100-cariable variable.
There are three main types of arrays, and an ID called an array index is used to access each array value.
Numeric Array
An array having indexes in numbers. Values are linearly stored and accessed.
These arrays can hold strings, integers, and other objects, but numbers will be used to represent their indexes. array indexes typically begin at zero.
Example:
here we have used arra() function to create a array.
<?php
/*first method to create a array in PHP, */
$function = array(1,2,3,4,4);
$foreach($numbers as $value)
echo "Value is $value </br>";
/*second method to create array in PHP.*/
$number[1]="one"
$number[2]="two"
$number[3]="three"
$number[4]="four"
$number[5]="five"
foreacg( $numbers as $value)
echo "values is $value <br />"
?>
Associate Array
An array holding the index string. Rather than keeping everything in strict linear index order, this method keeps element values along with key values.
The functionality of the association array is quite similar to that of numeric arrays, however their indexes are different. Since the index of an association array is a string, a clear connection between the key and value can be made.
A numerically indexed array would not be the ideal option to store employee salary in an array. Instead, we might use the key in our association array—the employees' names—and the value—their individual salaries.
note -When printing, do not enclose an associative array in double quotes since else, no value would be returned.
Example :-
<?php
$dept = array("bba"=>50,"bca"=>60,"mba"=>20,"bac"=>70)
echo "values,"<br/>"
foreach($dept as value)
echo $value."<br/>".;
foreach($dept as $x=>$y)
echo $x."".$y."<br/>";
?>
Multidimentional Array
Multidimensional array in PHP is also referred to as an array of arrays. It enables the array storage of tabular data. PHP multidimentional array can be represented in the form of metrix which is represented by two * column.
Example :-
<?php
$student =array(// A two dimentional array
array(1,"p1",40),
array(2,"p2",60),
array(3,"p3",70)
};
//One way
foreach($student as $val)
{
echo $val."";
echo"<br/'>";
}
//another way
for($r=0;$r<4;$r++)
{
for($c=0;$c<3;$c++
echo($student[$r][$c]."<br/>")
)
Example 2
<?php
$dept=array(
"BCA"=> array("progin C"=>40,"DS"=>55),
"BBA"=> array("HRM C"=>40,"FINNCE"=>55),
foreach($dept as $val=>$x)
{
each $val."<br/>";
foreach($x as $da=>$y)
echo $da."".$y."<br/>";
echo"<br/>";
}
?>
The Dynamic variable (Array)
Example $numberList = array();
for($i=0;$i10;$i++)
$numberList3[$i]=$i;
print_r($numberList);
Result array ([0]=>0[1]=>1[2]=>2[3]=>3[4]=>4[5]=>5[6]=>6[7]=7[8]=>9)
It's a little strange how print r prints an array. All it does is provide you with the element's key or index, followed by the value of the element. What you did was create an array with the values 0 through 9 in that order dynamically. When we iterated, or cycled through, the for loop, we added a new key and value pair to our new array.
Frequently Used PHP Arry Functions
<?php
//array__pop()
This function eliminates an element from the array's end.
$aArry =["a","b","c"];
array_pop($aArray);
print _r($aarray);
echo "<br/>";
//array_push()
this function adds a element to the end of an array
$aArry =["a","b","c"];
array_push($aArray,"b");
print _r($aarray);
echo "<br/>";
//array_shift()
This function eliminates a starting element from an array.
$aArry =["a","b","c"];
array_shift($aArray);
print _r($aarray);
echo "<br/>";
//array_unshift()
An element is added to the beginning of an array with this function.
$aArry =["a","b","c"];
array_unshift($aArray,"d");
print _r($aarray);
echo "<br/>";
//array_reverse()
this function reverses the order of elements of an array.
$data=array=(10.20.30.40);
print_r(array_reverse($data));
echo "<br/>"
Thanks for visiting our site please feel free to share your opinion in comment section
Tags:
php