A summary of methods for adding associated elements to the PHP array (associative array add Element)

Source: Internet
Author: User
Tags php tutorial

We are introduced here is in the array to increase the associative array, this is a multi-dimensional array, I would like to give you a few examples, I hope that the students will help you. In the"PHP Array add element Method Summary This article describes how to add elements to an array, then I want to add$arrayWhat about the elements of =array (' title ' = ' php tutorial ')? Array_push, Array_pop, Array_shift, array_unshift These functions are designed for index arrays of numeric types. To implement an associative array, you can use the Array_merge method or the + operator to begin by adding array element 1 to the array. Methods for adding array elements to PHP: (1) Adding array elements by assigning values:$states[' name ']= ' Tom ';(2) int Array_push (array target_array,mixed variable [, mixed variable ...]) function adds variable to target_ Returns true at the end of the array, or FALSE, where variable can be multiple. (3) int array_unshift (array target_array,mixed variable [, mixed variable ...]) function adds variable to target_array array header, returns true on success, Otherwise, false is returned, where variable can be multiple. All existing numeric keys are modified accordingly, and the associated keys are not affected. (4) Array array_pad (array Target_array,integer length,mixed pad_value) increases the size of the Target_array to the length specified. Is it OK to use Array_push or Array_unshift two methods? The answer is the specific method of negation: 1. Use the Array_merge method to implement a function code similar to Array_unshift to add elements at the beginning as follows to copy code <?php$queue= Array (' A ', ' B ');$queue= Array_merge (Array (' front ' = ' hello '),$queue);/*array ([Front] = Hello [0] + a [1] + b) */?>2.+ operator code copy Code as follows <?php$queue= Array (' A ', ' B ');$queue= Array (' front ' + = ' Hello ') +$queue;? > output is the same as using the Array_merge method. 3. Add the associated array element code at the end of the element as follows to copy the code <?php$queue= Array (' A ', ' B ');$queue[' front '] = ' hello ';/* output Array ([0] = a [1] = = b [front] = =) */?>

A summary of methods for adding associated elements to the PHP array (associative array add 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.