1. Adding elements to the array
PHP is a weakly typed language. Therefore, it is not necessary to declare the length for PHP array as in C language. The process of adding elements to them is also the process of declaring and initializing them.
Copy CodeThe code is as follows:
$capitals = Array (
' Alabama ' = ' Montgomery ',
' Alaska ' = ' Juneau ',
' Arizona ' = ' Phoenix '
);
It's also easy to continue adding elements
Copy CodeThe code is as follows:
$capitals [' Arkansas '] = ' Little Rock ';
If an array other than an associative array but only a numeric index can use the Array_push () and Array_unshift () functions to increase the element
2. Delete an element from the array
Removing elements from an array can use the unset () function
Copy CodeThe code is as follows:
unset ($capitals [' California ']);
You can also use the Array_pop () or Array_shift () functions to remove elements from the array header or trailer order
3, array key value interchange
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, in short, the key is reversed, you can use the Array_flip () function to complete the operation
Copy CodeThe code is 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 CodeThe code is 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 middle value of the array to lowercase and capitalize the first letter, it is a good idea to use the callback function to invoke each array member recursively, and this function is Php_map () in PHP.
Copy CodeThe code is 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. Sort by Array key
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
Ksort ($capitals);
7. Order of random array elements
Shuffle () and the above Ksort () function, on the contrary, can disrupt the existing order of the array to achieve the purpose of randomization.
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
Shuffle ($capitals);
8. If the lookup key or value exists
Find out if a value exists using the In_array () function
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
if (In_array ("Juneau", $capitals))
{
echo "exists!";
} else {
echo "Does not exist!";
}
Find out if a key exists using the array_key_exists () function
Copy CodeThe code is 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
It's a cliché, basically, to the Array_search () function.
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
$state = Array_search (' Juneau ', $capitals);
$state = ' Alaska '
10. Using the PHP standard function library
Take a breath to introduce this multi-action array function, and if you still feel uncomfortable, you can continue to view the contents of the standard PHP Library ^_^
Copy CodeThe code is 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
", $state, $capital);
}
The capital of Arizona is Phoenix
The capital of Alaska is Juneau
The capital of Alabama is Montgomery
http://www.bkjia.com/PHPjc/323652.html www.bkjia.com true http://www.bkjia.com/PHPjc/323652.html techarticle 1. Adding an element to an array PHP is a weakly typed language. Therefore, it is not necessary to declare the length for PHP array as in C language. The process of adding elements to them is also the process of declaring and initializing them. ...