What is an array?
When using PHP for development, you need to create many similar variables either early or late.
Without many similar variables, you can store data as elements in an array.
All elements in the array have their own IDs, so they can be easily accessed.
Join Array
Join array. Each ID key is associated with a value.
It is not the best practice to use a numeric array to store data related to specific named values.
By associating arrays, we can use values as keys and assign values to them.
This article will introduce 10 PHP-related Array Operations and 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.
$ Capitals = array (
'Babama' => 'mongomery ',
'Apache' => 'juneau ',
'Arizona '=> 'phoenix'
); 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 named function, such as array_push () and array_unshift () functions, but these functions cannot be used to associate arrays.
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, which are more flexible. You can use array_shift () and array_pop () the function deletes 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 as the associated value. The array_flip () function can easily complete this task.
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 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.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. 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.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 arrays by pressing the buttons
The flash card program uses various sorting methods. For example, you can use the ksort () function to sort the associated arrays in alphabetical order.Copy codeThe Code is as follows: $ capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
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.Copy codeThe Code is as follows: $ capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
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.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! ";
}
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.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
You may want to search for Array resources, so that you can easily search associated States using a specific state government. You can use the array_search () function to search for arrays.Copy codeThe Code is as follows: $ capitals = array (
'Arizona '=> 'phoenix ',
'Apache' => 'juneau ',
'Babama' => 'mongomery'
);
$ State = array_search ('juneau ', $ capitals );
// $ 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.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
This is only one of the many great functions of SPL. You must read the PHP documentation for more information.