Associative array - an essential data type used to describe a collection of unique key and associated values - is a pillar of all included PHP programming languages. In fact, associative arrays are so central that PHP supports dozens of functions and other features capable of manipulating data table in every conceivable way Web development tasks. This broad support can be a bit overwhelming for developers who are seeking the most effective way to manipulate charts in their applications. In this article I will offer 10 tips to help you to shred, slice and dice your data in countless ways.1. Adding table ElementsPHP is a weakly typed language which means that you're not forced to explicitly declare an array nor its size. Instead of this you can declare them and populate the table simultaneously: capitals $ = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = "Phoenix"); additional table elements can be added as follows:$ capitals ['Arkansas'] = "Little Rock". If you are dealing with numerically indexed arrays and would rather be preceded and add items to a function named explicitly, check array_push() and array_unshift() functions, these functions do not work with associative arrays). 2 Delete array ElementsTo remove an element of an array, use the unset() function: unset($capitals['California']); using numerically indexed arrays you have somewhat more flexibility in terms of remove items from the array that you can use the functions array_shift() and array_pop() to remove an item from the beginning and end of the array, respectively.3. Exchange ValuesSuppose and key you want to create a new array named $States, which would use the capitals of the State as the index names and status as the associated value. This task is accomplished easily using array_flip() function:$ cities = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ State = array_flip($capitals); / / declares $ = array (/ / = string "Alabama", "Montgomery" / / "Juneau" = string "Alaska", / / "Phoenix" = string "Arizona" / /); 4 Merger of ArraysSuppose preceding tables have been used in conjunction with a Web-based "flash card" service and you wish to offer students a way to test their knowledge of the capitals around the world, included States. You can merge tables containing capitals State and country using the array_merge() function:$ stateCapitals = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ countryCapitals = array ('Australia' = > 'Canberra', 'Austria' = > 'Vienna', 'Algeria' = "Algiers");$ capitals = array_merge ($ stateCapitals, $countryCapitals); 5 Table ValuesSuppose data found in a table potentially publishing contains errors of capitalization, and you want to correct these errors before to insert data into the database. You can use the array_map() function to implement a callback to each capitalize($element) function: array element {$ element = strtolower($element); ucwords($element); return} capitals $ = array ('Alabama' = "montGoMEry", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ capitals = array_map ("profit", $capitals);
[Next page]
You can post questions/comments/corrections here
If you are looking for help, please post to the appropriate here forum. Your questions will be respond much more quickly.
No comments:
Post a Comment