Php > Arrays
Arrays and Associative Arrays
Arrays and Associative Arrays Arrays are one of the most powerful parts of the PHP language.You can initialize your array in following manner. You can also declare using a list.$ar=array("Visual","Basic","Scripting"); Printing a List with commas array_splice array_splice is used to shrink an array.array_splice(array init,int start,int lenth,aray repl.) array_unique array_unique eliminates duplicate entries from your array. Returning the Current Element from an Array Use PHP in-built current function or loop through array. $current_element=current($ar); ?> count ===== function is used to count no of item in an array. foreach ======= php foreach($ar as $element) { print $element; } ?> Finding Elements in One Array but Not Another ============================================= Use array_diff() function to find the difference. php$ar1=array("Green","Red","White"); $ar2=array("Red","Blue","Violet"); $diff=array_diff($ar1,$ar2); foreach ($diff as $element){ print $element; print "
"; } ?> Appending an Array to another ============================= Use Built-in array_merge function. ";} ?> You can also + sign to merge the files.$add=$ar1+$ar2;If you dont want to lose elements that are same in both arrays, you can use the array_merge_recursive() function. Reversing an array ================== use built-in array_reverse function. "; } ?> ASSICIATIVE ARRAY ================= Associative array has "String Index Association". Associative array represent relationship.For example "Sachin is Cricket Star", you can represent it as $star['Cricket']="Sachin"; ?> Constructing associative array is similar to Normal array but you have to specify both the value and the key. $star=array("Cricket"=>"Sachin"); ?> Adding new elements in an associative array =========================================== "Cricket, "Anna"=>"Tenis", "Maradona"=>"Football"); $stars["MJ"]="BasketBall"; ?> Associative Array - Testing key presence ======================================== Use PHP's built-in function isset() "Cricket", "Anna" => "Tenis", "Maradona" => "Football"); if (isset($stars["Kapil"])) { echo "OK ..."; }else{ echo "Not Exist ..."; } ?> Deleting elements in an associative array ========================================= Use unset buit-in function for deleting n element , you can pass a single element or a whole array into it. "Cricket", "Anna" => "Tenis", "Maradona" => "Football"); unset($stars[Kapil],$stars[Anna]); foreach($stars as $nam=>$play){ print $nam; } ?> Moving through an Associative Array =================================== use foreach loop $value){ //operations } ?> *To merge two arrays you can use array_merge() function or simple use +sign $ar1+$ar2
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage