10 tips for associating arrays with PHP

Source: Internet
Author: User
An associative array is a data type that is often used to describe a group of keys and their associated values. It can be said that it is the pillar of all programming languages. Of course, PHP is no exception. In fact, associating arrays is very important for Web development tasks. PHP supports a variety of methods for associating arrays. for developers, it is best to find out the most effective method.

An associative array is a data type that is often used to describe a group of keys and their associated values. It can be said that it is the pillar of all programming languages. Of course, PHP is no exception. In fact, associating arrays is very important for Web development tasks. PHP supports a variety of methods for associating arrays. for developers, it is best to find out the most effective method.

  AssociationAn array describes a group of keys and theirAssociationA data type that is often used when values are used. It can be said that it is the pillar of all programming languages. Of course, PHP is no exception. In fact,AssociationArrays are very important for Web development tasks. PHP supports multiple operations.AssociationArray method, for developers, to find out the most effective, the most suitable method for their own development programs to manipulate these arrays.

This article will introduce 10 PHP operationsAssociationArrayTipsSkilled use can help you improve development efficiency.

1. Add array elements

PHP is a weak type language, which means you do not need to declare an array and its size. On the contrary, you can declare and fill the array at the same time.

1.$capitals=array(
2.'Alabama'=>'Montgomery',
3.'Alaska'=>'Juneau',
4.'Arizona'=>'Phoenix'
5. );

The additional array elements can be appended as follows:

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

If you are processing a numeric index array, you may want to use the frontend and append elements of the display naming function, such as the array_push () and array_unshift () functions, but these functions cannot be operated.AssociationArray.

2. Delete array elements

To delete an element from an array, use the unset () function, for example:

unset($capitals['California']);

When using a numeric index array, there are more ways to delete array elements. You can use the array_shift () and array_pop () functions to delete an element from the beginning and end of the array.

3. Exchange Keys and values

Suppose you want to create a new array named $ states, use the state government as the index, and use the state name asAssociationValue. The array_flip () function can easily complete this task.

1.$capitals=array(
2.'Alabama'=>'Montgomery',
3.'Alaska'=>'Juneau',
4.'Arizona'=>'Phoenix'
5. );
6.$states=array_flip($capitals);
7.// $states = array(
8.// 'Montgomery' => string 'Alabama',
9.// 'Juneau' => string 'Alaska',
10.// 'Phoenix' => string 'Arizona'
11.// );

4. Merge Arrays

If the preceding array is used by a Web-based "FlashCard" service, you can use array_merge () to test students' knowledge about the state capitals in the United States () functions merge arrays containing states and capitals.

1.$stateCapitals=array(
2.'Alabama'=>'Montgomery',
3.'Alaska'=>'Juneau',
4.'Arizona'=>'Phoenix'
5. );
6.$countryCapitals=array (
7.'Australia'=>'Canberra',
8.'Austria'=>'Vienna',
9.'Algeria'=>'Algiers'
10. );
11.$capitals=array_merge($stateCapitals,$countryCapitals);

5. Edit the array value

If the data in the array contains case-insensitive errors, you can use the array_map () function to apply a callback to each array element before inserting the data into the database.

function capitalize($element)
{
$element=strtolower($element);
returnucwords($element);
}
$capitals=array(
'Alabama'=>'montGoMEry',
'Alaska'=>'Juneau',
'Arizona'=>'phoeniX'
);
$capitals=array_map("capitalize",$capitals);

6. Sort arrays by pressing the buttons

Flash card programs often use various sorting methods. For example, you can use the ksort () function to sort data in alphabetical order.AssociationArray.

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.ksort($capitals);

Because the array is passed to the ksort () function through parameters, it means that you no longer need to assign the sorting result to another variable.

7. random array sorting

Another random sorting technique is involved in the flash card program. In this case, you need to use the shuffle () function to implement random sorting of array projects.

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.shuffle($capitals);

If you do not need to disrupt the array order and want to randomly select a value, use the array_rand () function.

8. Check whether the key and value exist.

You can use the in_array () function to determine whether an array element exists.

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.if (in_array("Juneau",$capitals))
7. {
8.echo"Exists!";
9. } else {
10.echo"Does not exist!";
11. }

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

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.if (array_key_exists("Alaska",$capitals))
7. {
8.echo"Key exists!";
9. } else {
10.echo"Key does not exist!";
11. }

9. Search for Arrays

You may want to search for Array resources, so that users can easily search by a specific StateAssociationYou can use the array_search () function to implement array search.

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.$state=array_search('Juneau',$capitals);
7.// $state = 'Alaska'

10. Standard PHP Library

The Standard PHP Library (SPL) provides developers with many data structures, iterators, interfaces, exceptions, and other functions not available in the previous PHP language, these functions can be used to traverse arrays through object-oriented syntax.

1.$capitals=array(
2.'Arizona'=>'Phoenix',
3.'Alaska'=>'Juneau',
4.'Alabama'=>'Montgomery'
5. );
6.$arrayObject=new ArrayObject($capitals);
7.foreach ($arrayObjectas$state=>$capital)
8. {
9.printf("The capital of %s is %s
",$state,$capital);
10. }
11.// The capital of Arizona is Phoenix
12.// The capital of Alaska is Juneau
13.// The capital of Alabama is Montgomery

This is only one of the many great functions of SPL. You must read the PHP documentation for more information.

Http://www.phpbuilder.com/columns/associative-array-manipulation/Jason_Gilmore12092010.php3

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.