Nothing Special   »   [go: up one dir, main page]

PHPAss 3 Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

KSV Msc IT Roll no -134

PHP Assignment-3 KSV DIV-2 -------All Array Function in one Program-----<?php echo "<h1>Array Functions</h1><hr color=black>"; echo "-Remember ,Keys must be unique and keys are case sensative.<br>"; $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat"); $arr2=Array("r"=>"Red","g"=>"Green","b"=>"Blue","c"=>"Cow "); //Array_chang_key_case echo "<hr color=black><h3>Array_change_key_case</h3> "; echo "-Changes the case of array keys and if any two keys are same then last key's value will be overwriiten to first one's value and only first key and its value will be displayed<br>"; echo "<br>"; print_r($arr); echo "<br>"; echo "Syntax : Array_change_key_case(Array,CASE) <br> Case : CASE_UPPER or CASE_LOWER<br>"; $ar=array_change_key_case($arr,CASE_UPPER); print_r($ar); //Array_combine $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat"); $arr2=Array("r"=>"Red","g"=>"Green","b"=>"Blue"); echo "<hr color=black><h3>Array_combine</h3> "; echo "-Combines two array.Here Values of First array becomes Key of new array and Values of second array becomes values of new array<br>"; echo "<br>"; print_r($arr); echo "<br>"; print_r($arr2); echo "<br>Syntax : Array_combine(arr1,arr2) Returns Array <br> "; $ar=array_combine($arr,$arr2); print_r($ar);

//Arrray_merge $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat"); $arr2=Array("r"=>"Red","g"=>"Green","b"=>"Blue","c"=>"Cow "); echo "<hr color=black><h3>Array_merge</h3> "; echo "-merges two array.Simply combines two array and if any key gets repeated then last key's value will be

hardik_c_patel@ymail.com

KSV Msc IT Roll no -134 overwriiten to first one's value and only first key and its value will be displayed<br>"; echo "<br>"; print_r($arr); echo "<br>"; print_r($arr2); echo "<br>Syntax : Array_merge(arr1,arr2) Returns Array <br> "; $ar=array_merge($arr,$arr2); print_r($ar); //Array_keys $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat"); echo "<hr color=black><h3>Array_keys</h3> "; echo "-keys of old array becomes values of new array"; echo "<br>"; print_r($arr); echo "<br>Syntax : Array_keys(arr) Returns Array <br> "; $ar=array_keys($arr); print_r($ar); //Array_values $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat"); echo "<hr color=black><h3>Array_values</h3> "; echo "-Values of old array becomes values of new array"; echo "<br>"; print_r($arr); echo "<br>Syntax : Array_values(arr) Returns Array <br> "; $ar=array_values($arr); print_r($ar); //Array_count_values $arr=Array("a","b","c","a","b","c"); echo "<hr color=black><h3>Array_count_values</h3> "; echo "-returns Array in which old arrays values becomes keys and no occurance of that values becomes Values"; echo "<br>"; print_r($arr); echo "<br>Syntax : Array_count_values(arr) Returns Array <br> "; $ar=array_count_values($arr); print_r($ar); //Array_key_exists $arr=Array("a","b","c","a","b","c"); echo "<hr color=black><h3>Array_key_exists</h3> "; echo "-returns True if given key exists else False"; echo "<br>"; print_r($arr); echo "<br>Syntax : Array_key_exists(key,arr) Returns Bool <br> "; echo array_key_exists("0",$arr);

hardik_c_patel@ymail.com

KSV Msc IT Roll no -134 //In_array $arr=Array("a","b","c","a","b","c"); echo "<hr color=black><h3>in_array</h3> "; echo "-returns True if given Value exists else False"; echo "<br>"; print_r($arr); echo "<br>Syntax : in_array(value,arr) Returns Bool <br> "; echo in_array("a",$arr); //Array_search $arr=Array("a","b","c","a","b","c"); echo "<hr color=black><h3>Array_search</h3> "; echo "-returns <b>Key</b> if given Value exists else False"; echo "<br>"; print_r($arr); echo "<br>Syntax : Array_serch(value,arr) Returns key <br> "; echo Array_search("c",$arr); //ksort $arr=Array("c"=>"Cat","b"=>"Ball","a"=>"Apple"); echo "<hr color=black><h3>ksort</h3> "; echo "Sorts Array Element Key Wise"; echo "<br>"; print_r($arr); echo "<br>Syntax : ksort(arr) <br> "; ksort($arr); print_r($arr);

//krsort $arr=Array("c"=>"Cat","b"=>"Ball","a"=>"Apple"); echo "<hr color=black><h3>krsort</h3> "; echo "Sorts Array Element Key Wise with Reverse order"; echo "<br>"; print_r($arr); echo "<br>Syntax : krsort(arr) <br> "; krsort($arr); print_r($arr);

//asort $arr=Array("c"=>"Cat","b"=>"Ball","a"=>"Apple"); echo "<hr color=black><h3>asort</h3> "; echo "Sorts Array Element value Wise"; echo "<br>"; print_r($arr); echo "<br>Syntax : asort(arr) <br> "; asort($arr); print_r($arr);

hardik_c_patel@ymail.com

KSV Msc IT Roll no -134 //rsort $arr=Array("c"=>"Cat","b"=>"Ball","a"=>"Apple"); echo "<hr color=black><h3>rsort</h3> "; echo "Sorts Array Element Value Wise in reverse order"; echo "<br>"; print_r($arr); echo "<br>Syntax : rsort(arr) <br> "; rsort($arr); print_r($arr); //array_chunk $arr=Array("a"=>"Apple","b"=>"Ball","C"=>"Cat","d"=>"Dog" ,"e"=>"Elephant","f"=>"Freeze","g"=>"Got"); echo "<hr color=black><h3>Array_chunk</h3> "; echo "gives Subarrays combined within main array with given no of chunks"; echo "<br>"; print_r($arr); echo "<br>Syntax : array_chunk(arr,ChunkSizeInt,PreserveKeyBool) Returns Array <br> "; $nr=array_chunk($arr,2,true); print_r($nr); //implode $arr=Array("a","b","C","d","e","f","g"); echo "<hr color=black><h3>implode</h3> "; echo "Converts Array into String"; echo "<br>"; print_r($arr); echo "<br>Syntax : implode(SepratorString,arr) Returns String <br> "; $str=implode("/",$arr); echo $str; //explode $str="a.b.c.d.e.f.g"; echo "<hr color=black><h3>explode</h3> "; echo "Converts string into Array"; echo "<br>"; echo "String is =".$str; echo "<br>Syntax : explode(SepratorString,str) Returns Array <br> "; $arr=explode(".",$str); print_r($arr); //extract $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat","d"=>"Dog" ,"e"=>"Elephant","f"=>"Freeze","g"=>"Got"); echo "<hr color=black><h3>extract</h3> "; echo "makes variables with name as key with value as value for tha key in memory"; echo "<br>"; hardik_c_patel@ymail.com

KSV Msc IT Roll no -134 print_r($arr); echo "<br>Syntax : extract(arr) Returns Nothing but makes variable in memory <br> "; extract($arr); echo "<br> a=".$a; echo "<br> b=".$b; echo "<br> c=".$c; //Array_reverse $arr=Array("a"=>"Apple","b"=>"Ball","c"=>"Cat","d"=>"Dog" ,"e"=>"Elephant","f"=>"Freeze","g"=>"Got"); echo "<hr color=black><h3>Array_reverse</h3> "; echo "Arranges array element into reverse order and returns tha new Array"; echo "<br>"; print_r($arr); echo "<br>Syntax : array_reverse(arr) Returns array <br> "; $ar=array_reverse($arr); print_r($ar); //Array_diff $arr=Array("a","b","c","d"); $arr2=Array("a","b","c","e"); echo "<hr color=black><h3>Array_diff</h3> "; echo "compares both array and return those elements which are in first array but not in second"; echo "<br>"; print_r($arr); echo "<br>"; print_r($arr2); echo "<br>Syntax : array_diff(arr,arr2) Returns array <br> "; print_r(array_diff($arr,$arr2)); //Array_rand $arr=Array("a"=>"Apple","b"=>"Bag","c"=>"cat"); echo "<hr color=black><h3>Array_rand</h3> "; echo "selects n number of random keys from given arrays and puts them as value into new array"; echo "<br>"; print_r($arr); echo "<br>Syntax : array_rand(arr,nInt) Returns array <br> "; print_r(array_rand($arr,2)); //Array_slice $arr=Array("a"=>"Apple","b"=>"Bag","c"=>"cat","d"=>"Dog", "e"=>"Elephant","f"=>"Freeze","g"=>"Got"); echo "<hr color=black><h3>Array_slice</h3> "; echo "Works like substring in string functions"; echo "<br>"; print_r($arr);

hardik_c_patel@ymail.com

KSV Msc IT Roll no -134 echo "<br>Syntax : array_slice(arr,StartIndexInt,LengthInt,PreserveKeyBool) Returns array <br> "; echo "<br><br>print_r(array_slice($arr,2))=="; print_r(array_slice($arr,2)); echo "<br><br>print_r(array_slice($arr,-2,1))=="; print_r(array_slice($arr,-2,1)); echo "<br><br>print_r(array_slice($arr,0,3))=="; print_r(array_slice($arr,0,3)); echo "<br><br>print_r(array_slice($arr,2,-1,true))=="; print_r(array_slice($arr,2,-1,true)); echo "<br><br>print_r(array_slice($arr,-4,-2))=="; print_r(array_slice($arr,-4,-2)); //Array_sum $arr=Array("a"=>10,"b"=>20,"c"=>30); echo "<hr color=black><h3>Array_sum</h3> "; echo "Calculates Numenical values of given array (Summation)"; echo "<br>"; print_r($arr); echo "<br>Syntax : array_sum(arr) Returns integer <br>" ; echo "Sum is =".array_sum($arr); //Array_unique $arr=Array("a"=>"A","b"=>"B","c"=>"C","d"=>"A","e"=>"E"); echo "<hr color=black><h3>Array_unique</h3> "; echo "Removes duplicate values and gives unique value Array "; echo "<br>"; print_r($arr); echo "<br>Syntax : array_unique(arr) Returns Array <br>" ; print_r(array_unique($arr)); //Array_flip $arr=Array("a"=>"A","b"=>"B","c"=>"C","d"=>"D","e"=>"E"); echo "<hr color=black><h3>Array_Flip</h3> "; echo " Interchanges values to keys and keys to Values"; echo "<br>"; print_r($arr); echo "<br>Syntax : array_flip(arr) Returns Array <br>" ; print_r(array_flip($arr)); ?>

hardik_c_patel@ymail.com

You might also like