Tuesday, 9 June 2020

arrays and associative arrays

arrays and associative arrays
<?php
//arrays
array is avariable which can storre more than one value
$users =  array();
$users = array ('rafigue', 'samir', 'vikram');
echo $users;
aray
print_r($users);
echo $users[2];
vikram

echo '<br><br>'.$users[0].'<br><br>;
$users[3] = 'anil';
print_r($users);
?>
indexed arrays


  1. <?php  
  2. $size=array("Big","Medium","Short");  
  3. echo "Size: $size[0], $size[1] and $size[2]";  
  4. ?>  


find the lenght of array

  1. <?php  
  2. $size=array("Big","Medium","Short");  
  3. echo count($size);  
  4. ?>  


associative arrays
$food = array ('briyani', 'pizza', 'salad');
normal array
$food = array ('briyani' =>500, 'pizza' =>200, 'salad'=>300);
second argument
echo $food('biryani');
500
print_r($food);
we wil give the key and value, key is biryani and 500 is value

echo $food(0);
undefined
we cannot print the associative array use echo and key o
multi dimensional array


for each
we cant print by accessing individual elemnt so use for each
$food = array ('briyani' =>500, 'pizza' =>200, 'salad'=>300);
assocuative array lefr side is keys and right side is value
foreach($food as $f => $rs){
foreach($arrayname as $value)
echo $f.'<br>';
echo $rs. '<br/>;
echo '<strong>'.$f.'<strong><br>';
print key in bold
echo '<strong>'.$f.'<strong> --rs.';
with string
how to load the values

food["biryani"] = "500";
food["pizza"] = "200";

how to access


  1. <?php    
  2. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");    
  3. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
  4. echo "John salary: ".$salary["John"]."<br/>";  
  5. echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
  6. ?>  


  1. <?php    
  2. $salary["Sonoo"]="350000";    
  3. $salary["John"]="450000";    
  4. $salary["Kartik"]="200000";    
  5. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
  6. echo "John salary: ".$salary["John"]."<br/>";  
  7. echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
  8. ?>    


Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000

  1. <?php    
  2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  
  3. foreach($salary as $k => $v) {  
  4. echo "Key: ".$k." Value: ".$v."<br/>";  
  5. }  
  6. ?>    
Key: Sonoo Value: 550000
Key: Vimal Value: 250000
Key: Ratan Value: 200000



multi dimensional array
students
boys
vamsi
krishna
sai
girls
sita
geeta
<?php
rosy
$students = array('boys' => array ('rafigue', 'vikram', 'srikant'), 'girls' => array('sita', 'geeta', 'rosy'));
boys is arraay name and rafigue is values

echo $students['boys'][0];
boys is arrray
?>
print the data in that array
mutli diensiona can be 2d 3d, 4d,5d etc
2d - $name[] []
3d - $name [] []
2d arrray
/*
$laptop = array(
array ("rahul", "dell", 10),
array in array called 2 d array
array ('sonam", "hp", 20),
array ("sumit", "zed", 30)
};

*/

$laptop [0][] = "rahul";
$laptop [0][] = 'dell';
$laptop[0][] = 10;


$laptop[1][] = "sonam";
$laptop[1][] = "sonvam";
$laptop[1][] = "sonasm";

$laptop[2][] = "sonasm1";
$laptop[2][] = "sonasm2";
$laptop[2][] = "sonasm3";

echo $laptop[2][2];
echo $laptop[0][0];
access the 2d arays
  1. $emp = array  
  2.   (  
  3.   array(1,"sonoo",400000),  
  4.   array(2,"john",500000),  
  5.   array(3,"rahul",300000)  
  6.   );  
Id Name Salary
1 sonoo 400000
2 john 500000
3 rahul 300000

  1. <?php    
  2. $emp = array  
  3.   (  
  4.   array(1,"sonoo",400000),  
  5.   array(2,"john",500000),  
  6.   array(3,"rahul",300000)  
  7.   );  
  8.   
  9. for ($row = 0; $row < 3; $row++) {  
  10.   for ($col = 0; $col < 3; $col++) {  
  11.     echo $emp[$row][$col]."  ";  
  12.   }  
  13.   echo "<br/>";  
  14. }  
  15. ?>    



1 sonoo 400000 
2 john 500000 
3 rahul 300000 



multi d associative array
if we know 2d aray , we can create simply 3d also
$students = array (
"vamsi" => array ("maths" => 100, "science" => 80, "social" => 75),
"krishna" => array ("maths" => 90, "science" => 100, "social" => 45),
"nvk" => array ("maths" => 80, "science" => 40, "social" => 25),
);
$students["vamsi"]["maths"] = 100;
$students["vamsi"]["science"] = 80;
$students["vamsi"]["social"] = 75;

$students["krishna"]["maths"] = 90;
$students["krishna"]["science"] = 100;
$students["krishna"]["social"] = 45;


$students["nvk"]["maths"] = 80;
$students["nvk"]["science"] = 40;
$students["nvk"]["social"] = 25;
20

 echo $students["vamsi"]["science"] ;
php sort

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. sort($season);  
  4. foreach$season as $s )    
  5. {    
  6.   echo "$s<br />";    
  7. }    
  8. ?>    


autumn
spring
summer
winter


array reverse
  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. $reverseseason=array_reverse($season);  
  4. foreach$reverseseason as $s )    
  5. {    
  6.   echo "$s<br />";    
  7. }    
  8. ?>    
.
autumn
spring
winter
summer



how to search in arrray

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. $key=array_search("spring",$season);  
  4. echo $key;    
  5. ?>    
2


how to return the matching elements of array


  1. <?php    
  2. $name1=array("sonoo","john","vivek","smith");    
  3. $name2=array("umesh","sonoo","kartik","smith");    
  4. $name3=array_intersect($name1,$name2);  
  5. foreach$name3 as $n )    
  6. {    
  7.   echo "$n<br />";    
  8. }    
  9. ?>    
1. <?php    
2. $season=array("summer","winter","spring","autumn");    
3. echo "Season are: $season[0], $season[1], $season[2] and $season[3]";    
Season are: summer, winter, spring and autumn
4. ?>    
1. <?php    
2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
Array ( [SONOO] => 550000 [VIMAL] => 250000 [RATAN] => 200000 )
3. print_r(array_change_key_case($salary,CASE_UPPER));   
4. ?>    
1. <?php    
2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
Array ( [sonoo] => 550000 [vimal] => 250000 [ratan] => 200000 )
3. print_r(array_change_key_case($salary,CASE_LOWER));   
4. ?>   
rray_chunk() function splits array into chunks.
1. <?php    
2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
Array (
3. print_r(array_chunk($salary,2));   
[0] => Array ( [0] => 550000 [1] => 250000 )
4. ?>    
[1] => Array ( [0] => 200000 )
)
how to count the number of elemens in array
1. <?php    
2. $season=array("summer","winter","spring","autumn");    
4
3. echo count($season);    
4. ?>    
  1. <?php  
  2.     $snacks = array('drinks' => array('cold coffee''traffic jam''Espresso',  
  3.     'Americano'), 'bevrage' => array('spring rolls''nuddles'));  
  4.     echo count($snacks, 1);  
  5.     echo "</br>";  
  6.     echo sizeof($snacks, 1);      
  7. ?>  

8

8




how to remove the elements in array


  1. <?php  
  2.     $color = array("Blue""Red""Black""Green""Gray""White");  
  3.     echo "Arraylist: ";  
  4.     print_r($color);  
  5.     $remove = array_shift($color);  
  6.     echo "</br>Removed element from array is: ";  
  7.     print_r($remove);  
  8.     echo "</br> Updated arraylist: ";  
  9.     print_r($color);  
  10. ?>     
Arraylist: Array ( [0] => Blue [1] => Red [2] => Black [3] => Green [4] => Gray [5] => White ) 
Removed element from array is: Blue
Updated arraylist: Array ( [0] => Red [1] => Black [2] => Green [3] => Gray [4] => White )




  1. <?php  
  2.     $game = array(1 => "Carom", 2 => "Chess", 3 => "Ludo");  
  3.     echo "Removed element: ".array_shift($game). "</br>";  
  4.     print_r($game);  
  5. ?>    
Removed element: Carom
Array ( [0] => Chess [1] => Ludo )


  1. <?php  
  2.     $numbers = array(25, 12, 65, 37, 95, 38, 12);  
  3.     $removed = array_shift($numbers);  
  4.     echo "Removed array element is: "$removed;   
  5.     echo "</br> Update array is: ";  
  6.     print_r($numbers);  
  7. ?>  
Removed array element is: 25
Update array is: Array ( 
[0] => 12 
[1] => 65 
2] => 37 
[3] => 95 
[4] => 38 
[5] => 12 
)




how to re




sonoo
smith

how to remove thelast element in arrya





  1. <?php  
  2.     $car = array("Mercedes""Creta""Audi""Chevrolet""Skoda");  
  3.     echo "Arraylist: ";  
  4.     print_r($car);  
  5.     $remove = array_pop($car);  
  6.     echo "</br>Removed element from array is: ";  
  7.     print_r($remove);  
  8.     echo "</br> Updated arraylist: ";  
  9.     print_r($car);  
  10. ?>     


Arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet [4] => Skoda ) 
Removed element from array is: Swift
Updated arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet )


  1. <?php  
  2.     $mall = array(1 => "DLF", 2 => "GIP", 3 => "V3S");  
  3.     echo "Removed element: ".array_pop($a). "</br>";  
  4.     print_r($mall);  
  5. ?>     
Removed element: Jupiter
Array ( [1] => DLF [2] => GIP )



  1. <?php  
  2.     $numbers = array(45, 76, 23, 91, 82, 39);  
  3.     $removed = array_pop($numbers);  
  4.     echo "Removed array element is: "$removed;   
  5.     echo "</br> Update array is: ";  
  6.     print_r($numbers);  
  7. ?>     
Removed array element is: 39
Update array is: Array ( [0] => 45 [1] => 76 [2] => 23 [3] => 91 [4] => 82 )


































array
print-r


echo
print_r
array
associative array
echo
print_r
how to whats this array or variable
how to print total data in array
how to print the data in array using index
how to add the names into an array
how to prints the data in array after newly added
what is difference between associative and normal array
whaat is meant by associative array and wahts is the key and value
how to access associateive array values
how to print the array
how to print the array 
2d array another rway representation
access array
multi dimensional 2d aassociative array
how to add some more elements into 2d arrays, and what is the alternative way to creat any arrays?
how to access or print 2d arrays
how to repreent 2d associative array
how to represent 2d associative arrays
how to acces 2d associatieve arrays




No comments:

Post a Comment