PHP: Common PHP Array manipulation functions

Source: Internet
Author: User

PHP provides us with a wealth of array manipulation functions, which can be very convenient to implement the functions we want to implement.


Adding and removing array elements

Ways to add elements

Array_unshift (Array array,mixed var [, mixed var ...])

Adding elements to the array header, All numeric keys will be modified accordingly, but the association keys will not be affected

Array_push (Array array,mixed var [, mixed var ...])

Add an element at the end of an array

Methods for deleting array elements

Array_shift (array array)

The array header element is deleted, and all numeric keys are modified accordingly, but the associated key is not affected and the value of the deleted element is returned

array_pop (array array)

Deletes the element at the end of the array and returns the value of the deleted element

========================================================================================

The key () function returns the keys of the current position of the array pointer

The current () function returns the value of the element where the pointer is currently positioned

Next () moves the array pointer to the next position and returns the value of the element if it is already the last position and returns False if the function is used again on the array

Prev () Moves the array pointer to the previous position and returns the value of the element, and if you use Prev () again on the array, the operation returns false for the first position.

End () moves the array pointer to the last position

Reset () Moves the array pointer to the first position

Use these functions to implement the functions of the Print_r () function

<?php//sequence Print array function  print_array_next ($arr) {if (!is_array ($arr)) {return  false;//not an array} Reset ($arr);//First set the array pointer to echo   ' array ('. Key ($arr). ' + '. Current ($arr). '   '; while (next ($arr)) {//next () moves the array pointer down one position and returns false//if it is already the last position, so it can be echo key as a loop condition ($arr). ' + '. Current ($arr). '   ';} echo  ') ';} Reverse print array Function  print_array_prev ($arr) {if (!is_array ($arr)) {return  false;//is not an array}end ($arr );//First move the array pointer to the last echo   ' array ('. Key ($arr). ' + '. Current ($arr). '   '; while (prev ($arr)) {//prev () moves the array pointer down one position, and returns false//if it is already the first position, Echo key ($arr) as a looping condition. ' + '. Current ($arr). '   ';} echo  ') ';} $arr  = array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5);p rint_array_next ($arr);echo  ' <br/> ';p rint_array_prev ($arr);? > Output Result:                 array (a=>1 b=>2 c=>3 d=>4 e=>5 ) &NBSP;&NBSp;              array (e=>5 d= >4 c=>3 b=>2 a=>1 )

===========================================================================================

===========================================================================================




This article is from the "Debug Me" blog, so be sure to keep this source http://zaitebie.blog.51cto.com/11179646/1742306

PHP: Common PHP Array manipulation functions

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.