PHP Array operation 10 tips for sharing _php tips

Source: Internet
Author: User
Tags arrays shuffle
1, add elements to the array
PHP is a weakly typed language. Therefore, you do not have to declare the length of the PHP array as the C language. The process of adding elements to them is also the process of declaring and initializing them.
Copy Code code as follows:

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

It's also simple to continue adding elements
Copy Code code as follows:

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

An array that is not an associative array but a numeric index can use the Array_push () and Array_unshift () functions to add elements
2. Remove elements from array
To remove an element from an array, you can use the unset () function
Copy Code code as follows:

unset ($capitals [' California ']);

You can also use the Array_pop () or Array_shift () function to remove an element from the array header or tail order
3, the array key value Exchange
If you want the key of the new array to be the value of the old array and the value is the key of the old array, simply to swap the key value, you can use the Array_flip () function to complete the operation
Copy Code code as follows:

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

4. Merging arrays
If you want to combine two or more arrays into a new array, the Array_merge () function can help with this busy ^_^
Copy Code code as follows:

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

5, modify the values in the array
For example, if you want to change the median value of an array to lowercase and capitalize the first letter, it's a good idea to recursively call each array member using the callback function, which is php_map in PHP ()
Copy Code code as follows:

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

6, according to the array of keys to sort the array
Copy Code code as follows:

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

7, the order of random array elements
Shuffle () and the above Ksort () function, on the contrary, can disrupt the existing order of the array, in order to achieve the purpose of randomization.
Copy Code code as follows:

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

8, find the key or whether the value exists
Find if there is a value using the In_array () function
Copy Code code as follows:

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

Find if there is a key using the array_key_exists () function
Copy Code code as follows:

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

9. Array Lookup
This is a cliché, basically all used to the Array_search () function
Copy Code code as follows:

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

10. Use PHP standard function library
To introduce the function of this multi-operation array, if you still feel not satisfied, you can continue to view the contents of the standard PHP Library ^_^
Copy Code code as follows:

$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 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.