PHP associative array 10 tips for _php tutorial

Source: Internet
Author: User
Tags spl shuffle
what is an array?
You will need to create many similar variables in the process of using PHP for development, either early or late.
Without a lot of similar variables, you can store the data in an array as an element.
Elements in the array have their own IDs, so they can be easily accessed.
Associative Arrays
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 a specific named value.
With associative arrays, we can use values as keys and assign values to them.
This article will cover 10 tips for manipulating PHP associative arrays, and skilled use can help you improve your development efficiency.
1. Adding array elements
PHP is a weakly typed language, which means you don't need to display an array and its size, 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 numeric index array, you may want to use the display named function to prepend and append elements, such as the Array_push () and Array_unshift () functions, but these functions cannot manipulate associative arrays.
2. Delete array elements
If you want to remove an element from an array, use the unset () function, such as:
unset ($capitals [' California ']); When using numeric indexed arrays, there are more and more flexible ways to delete array elements, and you can use the Array_shift () and Array_pop () functions to remove an element from the beginning and end of an array, respectively.
3. Exchange keys and Values
Suppose you want to create a new array called $states, use the state as the index, use the state name as the associated value, and use the Array_flip () function to accomplish this task easily.
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
Assuming that the preceding array is used by a web-based "flashcard" service, you want to provide a way to test students ' mastery of the U.S. state capitals, you can use the Array_merge () function to merge the arrays that contain the state and capital.
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. Edit Array Values
Assuming that the data in the array contains case errors, you want to correct these errors before inserting into the database, you can use the Array_map () function to apply a callback to each array element.
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 the keys in an array
Flashcard programs often use various sorts, such as alphabetical order, and you can use the Ksort () function keys to sort the associative array.
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
Ksort ($capitals);

Because the array is passed to the Ksort () function by argument, it means that you no longer need to assign the sort result to another variable.
7. Random Array sorting
Another random sorting technique is involved in the Flashcard program, when you use the shuffle () function to implement random ordering of array items.
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
Shuffle ($capitals);

If you do not need to scramble the array order, you just want to randomly select a value, then use the Array_rand () function.
8. Determine if 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 ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
if (In_array ("Juneau", $capitals))
{
echo "exists!";
} else {
echo "Does not exist!";
}

Very few people know that this function can also determine whether an array key exists, at this point, it is the same as the function of 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. Searching Arrays
You might want to search the array resources so that users can easily retrieve the associated state with a specific state, and you can use the Array_search () function to implement an array search.
Copy CodeThe code is as follows:
$capitals = Array (
' Arizona ' = ' Phoenix ',
' Alaska ' = ' Juneau ',
' Alabama ' = ' Montgomery '
);
$state = Array_search (' Juneau ', $capitals);
$state = ' Alaska '

10. Standard PHP Library
Standard PHP LIBRARY,SPL provides developers with a number of data structures, iterators, interfaces, exceptions, and other features not previously available in the PHP language, which can be used to iterate through an array using object-oriented syntax.
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

This is just one of the many great features of SPL, be sure to read the PHP documentation for more information.

http://www.bkjia.com/PHPjc/326330.html www.bkjia.com true http://www.bkjia.com/PHPjc/326330.html techarticle what is an array? You will need to create many similar variables in the process of using PHP for development, either early or late. Without a lot of similar variables, you can store the data as an element ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.