PHP Array
![]() |
| PHP Array |
What is Array
Arrays are complex variables that allow user to store multiple values or a group of values under a single variable name at a time.Examples: suppose you want to store multiple books in an array. Storing the books in a variable could look something like this:
<?php
$books=array("Think and Grow Rich","Law of Attraction","The Power of Now");
//echo Result From array
echo $books['0'].' | ';
echo $books['1'].' | ';
echo $books['2'];
?>
Output:Think and Grow Rich | Law of Attraction | The Powe of Now.
What is Array
There are three types of arrays in php.1.Indexed array --> An array with a numeric key.
Note:In an indexed array the indexes are automatically assigned and start with 0
2.Associative array --> An array where each key has its own specific value.
3.Multidimensional array --> An array containing one or more arrays within itself.
1.Indexed Array
![]() |
| PHP Indexed Array |
An indexed array stores each element with a numeric index.The indexes are automatically assigned and start with 0.
Examples 1:
<?php
$books=array("Think and Grow Rich","Law of Attraction","The Power of Now");
//echo Result From array
echo $books['0'].' | ';
echo $books['1'].' | ';
echo $books['2'];
?>
Output:Think and Grow Rich | Law of Attraction | The Powe of Now.
Examples 2:
<?php
$name=array("Anjan","Peter","David","Rakesh","Divya","Monpura");
//Counting Array Length
$count=count($name);
for($i=0;$i<$count;$i++){
echo $name[$i].' | ';
}
?>
Output:
Anjan | Peter | David | Rakesh | Divya | MonpuraExamples 3:
<?php
$name=array("Peter","David","Rock","Anjan");
$age=array("22","24","19","20");
$email=array("Peter@gmail.com","David@gmail.com","Rock@gmail.com","Anjan@gmail.com");
//Printing array as Table
?>
<table width="30%" cellspacing="0" border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Email</th>
</tr>
<?php
//counting total name
$count=count($name);
for($i=0;$i<$count;$i++){
?>
<tr>
<td><?php echo $name[$i];?></td>
<td><?php echo $age[$i];?></td>
<td><?php echo $email[$i];?></td>
</tr>
<?php }?>
</table>
Output:
| Name | Age | |
|---|---|---|
| Peter | 22 | Peter@gmail.com |
| David | 24 | David@gmail.com |
| Rock | 19 | Rock@gmail.com |
| Anjan | 20 | Anjan@gmail.com |
2.Associative array
Associative arrays are arrays that use named keys that you assign to them.Examples 1:
<?php $user=array( "name"=>"Anjan", "age"=>"18", "email"=>"Anjankumar@gmail.com" ); //Printing Value from array echo 'Your name is: '.$user['name'].'||'; echo 'Your You are: '.$user['age'].'||'; echo 'Your Contact is: '.$user['email']; ?>
Output: Your name is: Anjan || You are : 18 Years Old || Your Contact is: Anjankumar@gmail.com
Examples 2:
<?php
$age = array(
"Anjan Kumar"=>"20",
"Mr Ben"=>"37",
"Hrithik Rohsen"=>"43",
"ALexandra Dardario"=>"32",
"Mr Jack"=>"43",
"Peter"=>"53",
);
foreach ($age as $key => $value) {
echo 'Your Name is: '.$key .' and Your are '. $value .' Years Old';
}
?>
Output:
Your Name is: Anjan Kumar and Your are 20 Years Old
Your Name is: Mr Ben and Your are 37 Years Old
Your Name is: Hrithik Rohsen and Your are 43 Years Old
Your Name is: ALexandra Dardario and Your are 32 Years Old
Your Name is: Mr Jack and Your are 43 Years Old
Your Name is: Peter and Your are 53 Years OldExamples 3:
<?php
$user=array(
"Anjan Kumar"=>"Anjankumar@gmail.com",
"Raj Kumar"=>"Rajkumar@gmail.com",
"Bikash Kumar"=>"Bikashkumar@gmail.com"
);
?>
<table cellspacing="0" width="30%" border="1">
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php
foreach ($user as $name => $contact) {
?>
<tr>
<td><?php echo $name;?></td>
<td><?php echo $contact;?></td>
</tr>
<?php }
?>
</table>
Output:
| Name | |
|---|---|
| Anjan Kumar | Anjankumar@gmail.com |
| Raj Kumar | Rajkumar@gmail.com |
| Bikash Kumar | Bikashkumar@gmail.com |



No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.