Home  >  Article  >  Backend Development  >  10 Tips for PHP Array Operation_PHP Tutorial

10 Tips for PHP Array Operation_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:28:08730browse

1. Add elements to array
php is a weakly typed language. Therefore, there is no need to declare the length for php array like in C language. The process of adding elements to it is also a process of declaration and initialization.

Copy code The code is as follows:

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

Continuing to add elements is also very simple
Copy code The code is as follows:

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

If Arrays that are not associative arrays but only numerical indexes can use the array_push() and array_unshift() functions to add elements
2. Delete elements from the array
To remove elements from the array, you can use the unset() function
Copy code The code is as follows:

unset($capitals['California']);

Also Use the array_pop() or array_shift() function to sequentially remove elements from the head or tail of the array
3. Array key-value exchange
If you want the keys of the new array to be the values ​​of the old array and the values ​​to be the keys of the old array, In short, it is a key-value swap. You can use the array_flip() function to complete the operation
Copy the code The 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. Merge arrays
If you want to merge two or more arrays into a new array, the array_merge() function can help with this ^_^
Copy code The 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 all the median values ​​of the array to lowercase and then capitalize the first letter, use It is a good method to call the callback function recursively on each array member. This function in php is php_map()
Copy code The 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 the array according to the key of the array
Copy the code The code is as follows:

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

7. Randomize the order of array elements
shuffle() is exactly the opposite of the ksort() function above. Shuffle the existing order of the array to achieve randomization.
Copy code The code is as follows:

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

8. Find whether a key or value exists
To find whether a value exists or not, use the in_array() function
Copy the code The code is as follows:

$capitals = array(
'Arizona' => 'Phoenix',
'Alaska' => 'Juneau',
'Alabama' => 'Montgomery'
);
if (in_array("Juneau", $capitals))
{
echo "Exists!";
} else {
echo "Does not exist!";
}

To find whether a key exists, use the array_key_exists() function
Copy code The 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 search
This is a cliché. Basically, the array_search() function is used
Copy code The code is as follows:

$capitals = array(
'Arizona' => 'Phoenix',
'Alaska' => 'Juneau',
'Alabama' => 'Montgomery'
);
$state = array_search('Juneau', $capitals);
// $state = 'Alaska'

10. Use the PHP standard function library
to introduce this multi-operation array function in one go. If you still feel that it is not enough, you can continue to view the content in the Standard PHP Library ^_^
Copy code The 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323652.htmlTechArticle1. Adding elements to array PHP is a weakly typed language. Therefore, there is no need to declare the length for php array like in C language. The process of adding elements to it is also a process of declaration and initialization. Reply...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn