10 tips on PHP array Operations

Source: Internet
Author: User

1. add elements to array
Php is a weak language. Therefore, you do not need to declare the length for the php array like the C language. The process of adding elements to it is also a process of declaration and initialization.
Copy codeThe Code is as follows:
$ Capitals = array (
'Babama' => 'mongomery ',
'Apache' => 'juneau ',
'Arizona '=> 'phoenix'
);

It's easy to add more elements.
Copy codeThe Code is as follows:
$ Capitals ['arkansas '] = 'little Rock ';

You can use the array_push () and array_unshift () functions to add elements to arrays that are not associated with arrays but numeric indexes.
2. Delete elements from array
You can use the unset () function to remove elements from an array.
Copy codeThe Code is as follows:
Unset ($ capitals ['california ']);

You can also use the array_pop () or array_shift () function to remove elements from the array header or tail sequentially.
3. array key-value Interchange
If you want to create an array whose key is the value of the old array and whose value is the key of the old array, simply call the key-value pair, you can use the array_flip () function to complete the operation.
Copy codeThe Code is as follows:
$ Capitals = array (
'Babama' => 'mongomery ',
'Apache' => 'juneau ',
'Arizona '=> 'phoenix'
);
$ States = array_flip ($ capitals );
// $ States = array (
// 'Mongomery' => string 'abama ',
// 'Juneau '=> string 'alaska ',
// 'Phoenix '=> string 'arizona'
//);

4. Merge Arrays
If you want to combine two or more numbers into a new array, the array_merge () function can help ^_^
Copy codeThe Code is as follows:
$ StateCapitals = array (
'Babama' => 'mongomery ',
'Apache' => 'juneau ',
'Arizona '=> 'phoenix'
);
$ CountryCapitals = array (
'Australia '=> 'canonical ',
'Austria' => 'Vienna ',
'Algera' => 'algiers'
);
$ Capitals = array_merge ($ stateCapitals, $ countryCapitals );

5. Modify the value in array
For example, if you want to change all the values in the array to lowercase and then uppercase letters, it is a good method to recursively call each array member using the callback function. In php, this function is php_map ()
Copy codeThe Code is as follows:
Function capitalize ($ element)
{
$ Element = strtolower ($ element );
Return ucwords ($ element );
}
$ Capitals = array (
'Babama' => 'mongomery ',
'Apache' => 'juneau ',
'Arizona '=> 'phoenix'
);
$ Capitals = array_map ("capitalize", $ capitals );

6. Sort the array by the array key
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
Ksort ($ capitals );

7. randomize the order of array elements
The shuffle () function is opposite to the preceding ksort () function, which can disrupt the existing order of the array to achieve randomization.
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
Shuffle ($ capitals );

8. Check whether the key or value exists.
Use the in_array () function to check whether a value exists.
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
If (in_array ("Juneau", $ capitals ))
{
Echo "Exists! ";
} Else {
Echo "Does not exist! ";
}

Find whether the key exists using the array_key_exists () function
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
If (array_key_exists ("Alaska", $ capitals ))
{
Echo "Key exists! ";
} Else {
Echo "Key does not exist! ";
}

9. Search for Arrays
This is a commonplace. Basically, the array_search () function is used.
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
$ State = array_search ('juneau ', $ capitals );
// $ State = 'alaska'

10. Use the php standard function library
I will introduce this multi-Operation array Function in one breath. If you still feel uncomfortable, you can continue to view the content in Standard PHP Library ^_^.
Copy codeThe Code is as follows:
$ Capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
$ ArrayObject = new ArrayObject ($ capitals );
Foreach ($ arrayObject as $ state => $ capital)
{
Printf ("The capital of % s is % s <br/>", $ state, $ capital );
}
// The capital of Arizona is Phoenix
// The capital of Alaska is Juneau
// The capital of Alabama is Montgomery

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.