You should have mastered the use of PHP associative arrays _php Tips

Source: Internet
Author: User
Tags data structures shuffle

In the process of developing using PHP, sooner or later, you will need to create many similar variables, at which point you can store the data as an element in an array. The elements in an array have their own IDs, so you can easily access them.
Associative Arrays
An associative array in which each of its ID keys is associated with a value. Using a numeric array is not the best way to store data about a specific named value. By associative arrays, we can use values as keys and assign values to them.
Here are 10 tips for manipulating PHP associative arrays, which can help you improve your development efficiency.
1. Adding array elements
PHP is a weak type language, which means you don't need to display an array and its size, instead, you can declare and populate the array at the same time.

$capitals = Array ( 
 ' Alabama ' => ' Montgomery ', ' Alaska ' => ' Juneau ', ' Arizona ' => ' Phoenix ' 
); 

Additional array elements can be appended as follows:

$capitals [' Arkansas '] = ' Little rock '; 

If you are working on a digital index array, you may want to use the display named function to prepend and append elements, such as Array_push () and Array_unshift () functions, but these functions cannot manipulate associative arrays.
2. Delete array elements
If you want to remove an element from an array, use the unset () function, such as:

unset ($capitals [' California ']); 

When you use a numeric index array, you can remove an array element more and more flexibly, using the Array_shift () and Array_pop () functions to remove an element from the beginning and end of the array, respectively.
3. Exchange Keys and values
Suppose you want to create a new array called $states, use the state House as an index, use the state name as the associated value, and use the Array_flip () function to easily accomplish this task.

$capitals = Array ( 
 ' Alabama ' => ' Montgomery ', ' Alaska ' => ' Juneau ', ' Arizona ' => ' Phoenix ' 
); 
$states = Array_flip ($capitals); 
$states = Array ( 
///' Montgomery ' => string ' Alabama ', 
//' Juneau '   => string ' Alaska ',// 
' Phoe Nix '  => string ' Arizona ' 
//); 

4. Merging arrays
assuming that the preceding array is used by a web-based "flashcard" service, you want to provide a way to test students ' mastery of the state capitals of the United States, and you can use the Array_merge () function to merge arrays that contain states and capitals.

$stateCapitals = Array ( 
 ' Alabama ' => ' Montgomery ', ' 
 Alaska ' => ' Juneau ', 
 ' Arizona ' => ' Phoenix ') 
); 
$countryCapitals = Array ( 
 ' Australia ' => ' Canberra ', ' 
 Austria ' => ' Vienna '  , 
 ' Algeria '  = > ' Algiers ' 
); 
$capitals = Array_merge ($stateCapitals, $countryCapitals); 

5. Edit Array values
Assuming that the data in the array contains case errors, and you want to correct the errors before inserting them into the database, you can use the Array_map () function to apply a callback to each array element.

Function capitalize ($element) 
{ 
 $element = Strtolower ($element); 
 Return Ucwords ($element); 
$capitals = Array ( 
 ' Alabama ' => ' montGoMEry ', ' 
 Alaska ' => ' Juneau ', 
 ' Arizona ' => ' PhoeniX ') 
); 
$capitals = Array_map ("capitalize", $capitals); 

6, the key to the array of sorting
flashcard programs often use a variety of sorts, such as alphabetical order, and you can use the Ksort () function key to sort the associative array.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' Alaska ' => ' Juneau ', ' Alabama ' => ' Montgomery ' 
); 
Ksort ($capitals); 

Because arrays are passed by arguments to the Ksort () function, you no longer need to assign the result of the order to another variable.
7, random array ordering
another random sort technique is involved in the Flashcard program, where you use the shuffle () function to implement the random ordering of the array items.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' 
 Alaska ' => ' Juneau ', 
 ' Alabama ' => ' Montgomery ') 
); 
Shuffle ($capitals); 

If you don't need to scramble the array order, you just want to randomly select a value, then use the Array_rand () function.
8, determine whether the key and value exist
You can use the In_array () function to determine whether an array element exists.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' 
 Alaska ' => ' Juneau ', 
 ' Alabama ' => ' Montgomery ') 
); 
if (In_array ("Juneau", $capitals)) 
{ 
 echo "exists!"; 
} else { 
 echo "does not exist!"; 
} 

Few people know that this function can also determine whether an array key exists, and at this point it is the same as the function of the array_key_exists () function.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' Alaska ' => ' Juneau ', ' Alabama ' => ' Montgomery ' 
); 
if (array_key_exists ("Alaska", $capitals)) 
{ 
 echo "key exists!"; 
} else { 
 echo "key does not exist!"; 
} 

9. Search Array
you might want to search for array resources so that users can easily retrieve the associated States with a particular state, and you can do array searches by using the Array_search () function.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' 
 Alaska ' => ' Juneau ', 
 ' Alabama ' => ' Montgomery ') 
); 
$state = Array_search (' Juneau ', $capitals); 
$state = ' Alaska ' 

10. Standard PHP Library
The standard PHP library (Standard PHP LIBRARY,SPL) provides developers with a number of data structures, iterators, interfaces, exceptions, and other features not available in previous PHP languages that allow you to traverse an array through object-oriented syntax.

$capitals = Array ( 
 ' Arizona ' => ' Phoenix ', ' Alaska ' => ' Juneau ', ' Alabama ' => ' Montgomery ' 
); 
$arrayObject = new Arrayobject ($capitals); 
foreach ($arrayObject as $state => $capital) 
{ 
 printf ("The capital of%s is%s<br/>", $state, $capital); 
} 
The capital of Arizona are Phoenix 
//The capital of Alaska are Juneau 
//The capital of Alabama is Montgomery 

The above is 10 must have mastered the PHP associative array use technique, hope to be helpful to everybody's study.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.