Wednesday, 21 February 2018

Arrays in PHP

Array is used to store  multiple values in single variable  and it can hold more than one value at a time.


  Create an Array in PHP
array() function is used to create an array.
Advantage:
 We don't need to define multiple variables.so less code is required.
 We can sort the elements of array.so sorting is possible in PHP.
Types
There are 3 types of array in PHP.
1.     Indexed Array
2.     Associative Array
3.     Multidimensional Array

Numeric or Indexed Arrays
Numeric arrays use number as access keys.
Indexed Array can be assigned automatically and its index always starts at 0.
By default array elements are assigned to an index number.
We can store number, string and object in the PHP array.
There are two ways to define indexed array:
    1st way:
$name=array("kartik","satya","surya","dipak");  
2nd way:
$name[0]="kartik";  
$name[1]="satya";  
$name[2]="surya";  
$name[3]="dipak";  

Example:
<?php
$name=array("kartik","satya","surya","dipak"); 
echo "Employee name " . $name[0] . ", " . $name[1] .",".$name[2] ." and " . $name[3] . ".";
?>

Output:
Employee name  kartik, satya,surya and dipak.

Length of an Array
count() function is used to find length of an array in php.
Example:
<?php
$name=array("kartik","satya","surya","dipak"); 
echo "Length of Array: ";
echo count($name);
?>
Output:
Length of Array: 4

Array Example using for Loop

Example:
<?php
$name=array("kartik","satya","surya","dipak"); 
$arrlength=count($name);
for($i = 0; $i < $arrlength; $i++)
{
    echo $name[$i];
    echo "<br>";
}
?>
Output:
kartik
satya
surya
dipak

 PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
We can associate name with each array elements in PHP using => symbol.
There are two ways to create an associative array: 
     1st way:
$age=array("dipak"=>"27","satya"=>"45","Kartik"=>"20",","Surya"=>"25");  
2nd way:
$age["dipak "]="27";  
$age["satya "]="45";  
$age["Kartik"]="20";  
     $age["Surya"]="25";  
     
Example:
<?php   
$age=array("dipak"=>"27","satya"=>"45","Kartik"=>"20","Surya"=>"25");  

echo "Dipak age is: ".$age["dipak"]."<br/>"; 
echo " Satya age is: ".$age["satya"]."<br/>"; 
echo "Kartik age is: ".$age["Kartik"]."<br/>";
echo "Surya age is: ".$age["Surya"]."<br/>";
?>   
Output:
Dipak age is: 27
Satya age is: 45
Kartik age is: 20
Surya age is: 25

Multidimensional Arrays in PHP

 A multidimensional array is an array containing one or more arrays.
 A multidimensional array is also known as array of arrays
A multidimensional array can be represented in the form of matrix which is represented by row and column.

Definition
   $stu = array  
    (  
    array(1,"dipak",60),  
    array(2,"satya",70),  
    array(3,"kartik",78), 
    array(4,"Surya",90)  
     );  

Example:
<?php   
$stu = array 
  ( 
  array(1,"dipak",60), 
  array(2,"satya",70), 
  array(3,"kartik",78),
  array(4,"Surya",90) 
  ); 
  for ($row = 0; $row < 4; $row++) { 
  for ($col = 0; $col < 4; $col++) { 
    echo $stu[$row][$col]."  "; 
  } 
  echo "<br/>"; 
} 
?>   
Output:
1 dipak 60 
2 satya 70 
3 kartik 78 
4 Surya 90 


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...