php array
TRANSCRIPT
By Nikul Shah
Nikul Shah 1
Nikul Shah 2
An array is assigned to a single variable, but it can hold dozens of individual pieces of information.
Each individual bit of information, or row, is referred to as an array element.
Within every array element there are two parts: the value, which contains the actual information you want to store, and a unique key which identifies the value.
you will learn later how key palys an importanat role in the process of accessing and manipulating array elements.
Nikul Shah 3
Keys can either be non-negative integers or strings.
Array with integers as key are the most common type and known as SCALAR ARRAYs.
Those with key as string are known as ASSOCIATIVE ARRAY.
Nikul Shah 4
There are 2 kinds of arrays in php: indexed and associative arrray.
Key of indexed array is integer starts with 0.
Indexed array are used when we identify things with their position.
Associative arrays have strings as keys and behave more like two-columns table.
The first column is key and second is use to access value.
Nikul Shah 5
Numeric array
Associative Array
Multidimensional Array.
Nikul Shah 6
In numeric array have to create only elements, id will be automatically assigned to the elements.
example <?php $a = array("train", "car","bus"); print_r($a); ?> output Array ( [0] => train [1] => car [2] => bus )
Nikul Shah 7
In associative array we have create element with id.
example <?php $a =
array("6"=>"train","7"=>"car","8"=>"bus"); print_r($a); ?> Out put. Array ( [6] => train [7] => car [8] => bus )
Nikul Shah 8
Multidimensioanl array means we have to create array within array.
example
<?php
$a = array("a","b"=>array("train","car"),"bus");
print_r($a);
?>
Out put.
Array ( [0] => a [b] => Array ( [0] => train [1] => car ) [1] => bus )
Nikul Shah 9
Create an array by using the elements from one "keys" array and one "values" array:
<?php $fname=array("Peter","Ben","Joe"); $age=array("35","37","43"); $c=array_combine($fname,$age); print_r($c); ?>
Nikul Shah 10
This function is for counting the values of an array.
<?php
$no=array(0=>"5",1=>"10",2=>"15",3=>"15");
print_r(array_count_values($no));
?>
Nikul Shah 11
Compare the values of two arrays, and return the differences(difference value is displayes of first array.)
<?php
$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","b"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
print_r(array_diff($animal1,$animal3));
?>
Nikul Shah 12
Merge two arrays into one array:
<?php
$animal1=array("a"=>"lion","b"=>"tiger");
$animal2=array("c"=>"dog","d"=>"tiger");
$animal3=array("a"=>"cow","g"=>"tiger");
print_r(array_merge($animal1,$animal2,$animal3));
?>
Nikul Shah 13
Merge two arrays into one array:
<?php
$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
print_r(array_merge_recursive($animal1,$animal2
,$animal3));
?>
Nikul Shah 14
Returns an array in the reverse order <?php
$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
print_r(array_reverse($animal1));
?>
Nikul Shah 15
Searches an array for a given value and returns the key
<?php
$animal1=array("a"=>"lion","b"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
print_r(array_search("lion"$animal1));
?>
Nikul Shah 16
The array_sum() function returns the sum of all the values in the array.
<?php $a=array(5,15,25); echo array_sum($a); ?>
Nikul Shah 17
This function sorts an associative array in desending order, according to the value.
<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d
"=>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
arsort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>
Nikul Shah 18
This function sorts an associative array in ascending order, according to the value
<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=
>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
asort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>
Nikul Shah 19
This function sorts an associative array in desending order, according to the key.
<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=>"tiger
"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
arsort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>
Nikul Shah 20
This function sorts an associative array in ascending order, according to the key
<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=
>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
asort($animal1); foreach ($animal1 as $key =>$val) { echo "$key = $val"."<br>"; } ?>
Nikul Shah 21
This function is to find out no. of elements in an array.
<?php
$animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=>"tiger");
$animal2=array("c"=>"dog","d"=>"tiger");
$animal3=array("a"=>"cow","g"=>"tiger");
asort($animal1);
print_r(sizeof($animal1));
?>
Nikul Shah 22
Removes the first element from an array and returns the value of the removed element.
<?php $animal1=array("a"=>"lion","b"=>"tiger","c"=>"dog","d"=
>"tiger"); $animal2=array("c"=>"dog","d"=>"tiger"); $animal3=array("a"=>"cow","g"=>"tiger");
echo array_shift($animal1); echo "<br>"; print_r($animal1); ?>
Nikul Shah 23
Return an array of random keys:
<?php
$input=array("a","b","c","d","e","f","g","h",);
$rand_keys = array_rand($input,2);
echo $input[$rand_keys[0]]."\n";
echo $input[$rand_keys[1]]."\n";
?>
Nikul Shah 24
array_push() function inserts one or more elements to the end of an array.
You can add one value, or as many as you like.
Even if your array has string keys, your added elements will always have numeric keys.
Nikul Shah 25
<?php
$a=array("a"=>"tree",'b'=>"car");
array_push($a,"bike","activa");
print_r($a);
?>
Nikul Shah 26
array_pop() function deletes the last element of an array.
<?php
$a=array("tree","car","bike");
array_pop($a);
print_r($a);
?>
Nikul Shah 27