PHP Array(Index Array,Associative Array,Multidimensional Array) - Web Development and Web Design Codes

Latest

Friday, February 2, 2018

PHP Array(Index Array,Associative Array,Multidimensional Array)

PHP Array

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
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 Indexed array
<?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:Loop in Indexed Array:
<?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 | Monpura

Examples 3:Combine Multiple array as Table:
<?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 Email
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:Associative Array:
<?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:Loop in Associative Array:
<?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 Old

Examples 3:Loop in Associative Array: Learn how to create Table in Associative Array.
<?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 Email
Anjan Kumar Anjankumar@gmail.com
Raj Kumar Rajkumar@gmail.com
Bikash Kumar Bikashkumar@gmail.com

3.Multidimensional array

A multidimensional array is an array containing one or more arrays.In this lesson we will learn 2 Dimensional Array. A two-dimensional array is an array of arrays

Learn Multidimensional Arrays

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.