10 tips for manipulating PHP associative arrays

  • 2020-05-27 04:38:41
  • OfStack

What is an array?
In the course of developing with PHP, sooner or later, you will need to create many similar variables.
Without many similar variables, you can store data as elements in an array.
The elements in the array have their own ID, so they can be easily accessed.
An associative array
An associative array in which each ID key is associated with a value.
Using numeric arrays is not the best practice when storing data about specifically named values.
With associative arrays, we can take values as keys and assign values to them.
This article will introduce you to 10 techniques for manipulating PHP associative arrays that will help you develop more efficiently.
1. Add array elements
PHP is a weakly typed language, which means you don't need to display an array and its size to declare it. Instead, you can declare and populate the array at the same time.
$capitals = array(
'Alabama' = > 'Montgomery',
'Alaska' = > 'Juneau',
'Arizona' = > 'Phoenix'
); Additional array elements can be appended as follows:
$capitals [' Arkansas] = 'Little Rock'; If you are working with a numerically indexed array, you may want to use named display functions such as array_push() and array_unshift(), but these functions cannot manipulate associative arrays.
2. Delete array elements
To delete an element from an array, use the unset() function, such as:
unset ($capitals [' California ']); When you use a digitally indexed array, you have more flexibility in removing elements from the array. You can use the array_shift() and array_pop() functions to remove an element from the beginning and end of the array, respectively.
3. Swap keys and values
Let's say you want to create a new array called $states, with the state index and the state name as the associated value, and it's easy to 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' 
// ); 

4. Merge the array
Assuming that the previous array is used by an Web-based "FlashCard" service, you want to provide a way to test students' mastery of state capitals in the United States. You can use the array_merge () function to merge the state and capital arrays.
 
$stateCapitals = array( 
'Alabama' => 'Montgomery', 
'Alaska' => 'Juneau', 
'Arizona' => 'Phoenix' 
); 
$countryCapitals = array ( 
'Australia' => 'Canberra', 
'Austria' => 'Vienna', 
'Algeria' => 'Algiers' 
); 
$capitals = array_merge($stateCapitals, $countryCapitals); 

5. Edit array values
Assuming that the data in the array contains capitalization errors that you want to correct before inserting into the database, you can use the array_map() function to apply 1 callback to each array element.
 
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 by pressing the key
The FlashCard program often USES a variety of sorts, such as alphabetical order, you can use the ksort() function to sort associative arrays.
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
ksort($capitals); 

Because the array is passed to the ksort() function as an argument, you no longer need to assign the sorted result to another variable.
7. Random number group sorting
Another random-sort technique is involved in the FlashCard program, where you use the shuffle() function to randomly sort array items.
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
shuffle($capitals); 

If you don't need to shuffle the array, and you just want to randomly select a value, use the array_rand() function.
8. Determine whether keys and values exist
You can use the in_array() function to determine if an array element exists.
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
if (in_array("Juneau", $capitals)) 
{ 
echo "Exists!"; 
} else { 
echo "Does not exist!"; 
} 

Few people know that this function can also determine the existence of an array key. At this point, it does exactly the same thing as 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. Search an array
You may want to search for an array of resources so that the user can easily retrieve the associated state with a specific state, which can be done using the array_search() function.
 
$capitals = array( 
'Arizona' => 'Phoenix', 
'Alaska' => 'Juneau', 
'Alabama' => 'Montgomery' 
); 
$state = array_search('Juneau', $capitals); 
// $state = 'Alaska' 

10. Standard PHP library
The standard PHP library (Standard PHP Library, SPL) provides developers with many data structures, iterators, interfaces, exceptions, and other features not previously available in the PHP language that can be used to traverse groups through object-oriented syntax.
 
$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 

This is just one of the many great features of SPL, and you should definitely read the PHP documentation for more information.

Related articles: