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
find the lenght of array
|
|
$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
Sonoo salary: 350000 John salary: 450000 Kartik salary: 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 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
|
|||||||||||||
autumn spring summer winter
array reverse
- <?php
- $season=array("summer","winter","spring","autumn");
- $reverseseason=array_reverse($season);
- foreach( $reverseseason as $s )
- {
- echo "$s<br />";
- }
- ?>
autumn spring winter summer
how to search in arrray
- <?php
- $season=array("summer","winter","spring","autumn");
- $key=array_search("spring",$season);
- echo $key;
- ?>
2how to return the matching elements of array
- <?php
- $name1=array("sonoo","john","vivek","smith");
- $name2=array("umesh","sonoo","kartik","smith");
- $name3=array_intersect($name1,$name2);
- foreach( $name3 as $n )
- {
- echo "$n<br />";
- }
- ?>
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. ?>
- <?php
- $snacks = array('drinks' => array('cold coffee', 'traffic jam', 'Espresso',
- 'Americano'), 'bevrage' => array('spring rolls', 'nuddles'));
- echo count($snacks, 1);
- echo "</br>";
- echo sizeof($snacks, 1);
- ?>
88how to remove the elements in array
- <?php
- $color = array("Blue", "Red", "Black", "Green", "Gray", "White");
- echo "Arraylist: ";
- print_r($color);
- $remove = array_shift($color);
- echo "</br>Removed element from array is: ";
- print_r($remove);
- echo "</br> Updated arraylist: ";
- print_r($color);
- ?>
how to reArraylist: 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 )
- <?php
- $game = array(1 => "Carom", 2 => "Chess", 3 => "Ludo");
- echo "Removed element: ".array_shift($game). "</br>";
- print_r($game);
- ?>
Removed element: Carom Array ( [0] => Chess [1] => Ludo )
- <?php
- $numbers = array(25, 12, 65, 37, 95, 38, 12);
- $removed = array_shift($numbers);
- echo "Removed array element is: ". $removed;
- echo "</br> Update array is: ";
- print_r($numbers);
- ?>
Removed array element is: 25 Update array is: Array ( [0] => 12 [1] => 65 2] => 37 [3] => 95 [4] => 38 [5] => 12 )sonoo smithhow to remove thelast element in arrya
- <?php
- $car = array("Mercedes", "Creta", "Audi", "Chevrolet", "Skoda");
- echo "Arraylist: ";
- print_r($car);
- $remove = array_pop($car);
- echo "</br>Removed element from array is: ";
- print_r($remove);
- echo "</br> Updated arraylist: ";
- print_r($car);
- ?>
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 )
- <?php
- $mall = array(1 => "DLF", 2 => "GIP", 3 => "V3S");
- echo "Removed element: ".array_pop($a). "</br>";
- print_r($mall);
- ?>
Removed element: Jupiter Array ( [1] => DLF [2] => GIP )
- <?php
- $numbers = array(45, 76, 23, 91, 82, 39);
- $removed = array_pop($numbers);
- echo "Removed array element is: ". $removed;
- echo "</br> Update array is: ";
- print_r($numbers);
- ?>
Removed array element is: 39 Update array is: Array ( [0] => 45 [1] => 76 [2] => 23 [3] => 91 [4] => 82 )
| array | ||||||||||
| print-r | ||||||||||
|
||||||||||
| print_r | ||||||||||
| array | ||||||||||
| associative array | ||||||||||
| echo | ||||||||||
print_r
|
| 2d array another rway representation | ||||||
| access array | ||||||
multi dimensional 2d aassociative array
|
||||||
No comments:
Post a Comment