PHP array 10 tips to share

  • 2020-05-07 19:23:30
  • OfStack

1. Add elements to array
php is a weakly typed language. Therefore, it is not necessary to declare a length for php array, as in the c language. The process of adding elements to it is also the process of declaring and initializing.
 
$capitals = array( 
'Alabama' => 'Montgomery', 
'Alaska' => 'Juneau', 
'Arizona' => 'Phoenix' 
); 

It's also easy to keep adding elements
 
$capitals['Arkansas'] = 'Little Rock'; 

If an array is not an associative array but is only numerically indexed, you can add elements using the array_push() and array_unshift() functions
2. Remove elements from array
You can remove elements from an array using the unset() function
 
unset($capitals['California']); 

You can also remove elements from the array header or tail order using the array_pop() or array_shift() functions
3. array key value swap
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, which is simply a key-value swap, you can do this using the array_flip() function
 
$capitals = array( 
'Alabama' => 'Montgomery', 
'Alaska' => 'Juneau', 
'Arizona' => 'Phoenix' 
); 
$states = array_flip($capitals); 
// $states = array( 
// 'Montgomery' => string 'Alabama', 
// 'Juneau' => string 'Alaska', 
// 'Phoenix' => string 'Arizona' 
// ); 

Merge arrays
If you want to combine two or more arrays into a new array, the array_merge() function can help with this
 
$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 array
For example, if you want to change the median of an array to lowercase and uppercase, it's a good idea to recursively call each member of the array using a callback function, which in php is php_map()
 
function capitalize($element) 
{ 
$element = strtolower($element); 
return ucwords($element); 
} 
$capitals = array( 
'Alabama' => 'montGoMEry', 
'Alaska' => 'Juneau', 
'Arizona' => 'phoeniX' 
); 
$capitals = array_map("capitalize", $capitals); 

Sort the array according to the key of array
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
ksort($capitals); 

7. Randomize the order of array elements
shuffle(), in contrast to ksort() above, scrambles the existing order of the array to achieve randomization.
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
shuffle($capitals); 

8. Find whether the key or value exists
Use the in_array() function to find if a value exists
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
if (in_array("Juneau", $capitals)) 
{ 
echo "Exists!"; 
} else { 
echo "Does not exist!"; 
} 

To find out if there is a key, use the array_key_exists() function
 
$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 cliche, and it's pretty much a function of array_search()
 
$capitals['Arkansas'] = 'Little Rock'; 
0
Use the php standard library
This multi - operation array function, if you still feel not satisfied, you can continue to see Standard PHP Library content ^_^
 
$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 

Related articles: