Array:
-Array in PHP is the type of data structure that store multiple elements of similar data types under a single variable
-Array creates a list of elements of similar types that can be accessed [] of the key.
-An array is created using array() function in PHP
[Array in PHP:
Array is a collection of similar data types]
Types of Array in PHP:
- Index or Numeric Array
- Associative Array
- Multidimensional Array
1.Index Array:-
-Index array represented by the numbers, Numeric index array start from 0.
-Index arrays use number or index as the access key.
-An access key is a reference to memory slot in an array variable.
-The access key is used whenever we want to read or assign a new value to an array element.
Syntax:
<?php $variable_name[n]=value; ?>
Example:
<!DOCTYPE html> <html> <head> <title>cwipedia.in</title> </head> <body> <?php $a[0]="Hi"; $a[1]="Hey"; echo "<br>Index Array<br>"; echo $a[0],"<br/>",$a[1]; ?> </body> </html>
2.Associative Array:-
-An associative array is similar to index array but instead of linear storage and every value can be assigned with user-defined by the key of a string type.
-An array with String index where instead of linear storage, each value can be assigned a specific key.
-Associative array differs from the numeric array in the sense that associative array uses descriptive names for id keys.
Syntax:
<?php $variable_name=$array(Keyname => value) ?>
Example:
<!DOCTYPE html> <html> <head> <title>cwipedia.in</title> </head> <body> <?php $c=array("CSE"=>"101","CIVIL"=>"102"); echo "<br>Associative Array"; echo "<br>CSE=",$c["CSE"],"<br>CIVIL=",$c["CIVIL"]; ?> </body> </html>
3.Multidimensional Array:-
-Multidimensional contain single or multiple arrays that can be accessed multiple indices or key.
-In this type can create one dimensional and multidimensional array.
-The advantage of multidimensional arrays is that they allow us to group related data together.
Syntax:
?php $variable_name=array( array(element....), array(element....), . . . ) ?>
Example:
<!DOCTYPE html> <html> <head> <title>cwipedia.in</title> </head> <body> <?php $person=array( array("Name"=>"Shivam", "Email"=>"shivam@gmail.com"), array("Name"=>"XYZ", "Email"=>"xyz@gmail.com") ); echo "<br>Multidiamentional Array"; echo "<br>Shivam's Email Id:- ",$person[0]["Email"]; ?> </body> </html>
Comments
Post a Comment
If you have any query, please let us know