How to add and remove a PHP associative array

Source: Internet
Author: User
Tags arrays data structures spl

1. Adding array elements

PHP is a weak type 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. If you are working on a digital index array, you may want to use the display named function to prepend and append elements, such as 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, and when you use a numeric indexed array, you can remove the array elements more and more flexibly, and use the Array_shift () and Array_pop () functions to remove an element from the beginning and end of the array, respectively.

3. Exchange keys and values

Suppose you want to create a new array called $states, use the state House as an index, use the state name as the associated value, and use the Array_flip () function to easily accomplish this task.

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 state capitals of the United States, and you can use the Array_merge () function to merge arrays that contain states and capitals.

5. Edit Array values

Assuming that the data in the array contains case errors, and you want to correct the errors before inserting them into the database, you can use the Array_map () function to apply a callback to each array element.

6, the key to the array of sorting

Flashcard programs often use a variety of sorts, such as alphabetical order, and you can use the Ksort () function key to sort the associative array. Because arrays are passed by arguments to the Ksort () function, you no longer need to assign the result of the order to another variable.

7, random array ordering

Another random sort technique is involved in the Flashcard program, where you use the shuffle () function to implement the random ordering of the array items. If you don't need to scramble the array order, you just want to randomly select a value, then use the Array_rand () function.

8, determine whether the key and value exist

You can use the In_array () function to determine whether an array element exists.

9. Search Array

You might want to search for array resources so that users can easily retrieve the associated States with a particular state, and you can do array searches by using the Array_search () function.

10. Standard PHP Library

The standard PHP library (Standard PHP LIBRARY,SPL) provides developers with a number of data structures, iterators, interfaces, exceptions, and other features not available in previous PHP languages that allow you to traverse an array through object-oriented syntax. This is just one of the many great features of SPL, and be sure to read the PHP documentation for more information.

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.