Summary of some common addition, deletion, and insertion functions of arrays in PHP

Source: Internet
Author: User
This article mainly introduces some common functions for adding, deleting, and inserting arrays in PHP. array operations are the basic knowledge in PHP beginners, for more information, see. sometimes we need to expand an array or delete a part of the array. PHP provides some functions for extending and narrowing the array. For programmers who want to simulate various queue implementations (FIFO and LIFO), these functions can be convenient. As the name suggests, the function names (push, pop, shift, and unshift) of these functions clearly reflect their functions.

PS: The traditional queue is a data structure. Deleting elements is in the same order as adding elements, which is called FIFO or FIFO. On the contrary, the stack is another data structure, in which the order in which elements are deleted is the opposite of the order when they are added, which is either backward-forward, FIFO, or LIFO.

Add element in array header

The array_unshift () function adds elements to the array header. All existing numeric keys are modified accordingly to reflect their new positions in the array, but the associated keys are not affected. The format is as follows:

int array_unshift(array array,mixed variable[,mixed variable])

In the following example, two fruits are added before the $ fruits array:

$fruits = array("apple","banana");array_unshift($fruits,"orange","pear")// $fruits = array("orange","pear","apple","banana");

Add elements at the end of the array

The return value of the array_push () function is int type, which is the number of elements in the array after data is pressed. for this function, multiple variables can be passed as parameters and multiple variables can be pushed to the array at the same time. The format is:

(array array,mixed variable [,mixed variable...])

The following example adds two more fruits to the $ fruits array:

$fruits = array("apple","banana");array_push($fruits,"orange","pear")//$fruits = array("apple","banana","orange","pear")

Delete value from array header

The array_shift () function deletes and returns the elements found in the array. The result is that if a numeric value is used, all corresponding values are moved down, and the array using the correlated key is not affected. The format is:

mixed array_shift(array array)

The following example deletes the first element apple in the $ fruits array:

$fruits = array("apple","banana","orange","pear");$fruit = array_shift($fruits);// $fruits = array("banana","orange","pear")// $fruit = "apple";

Deletes an element from the end of an array.

The array_pop () function deletes the array and returns the last element of the array. The format is:

mixed array_pop(aray target_array);

The following example deletes the last state from the $ states array:

$fruits = array("apple","banana","orange","pear");$fruit = array_pop($fruits);//$fruits = array("apple","banana","orange");//$fruit = "pear";

Searching, filtering, and searching array elements are common functions of array operations. The following describes several related functions.

In_array () function

The in_array () function searches for a specific value in an array. if this value is found, true is returned. otherwise, false is returned. The format is as follows:
Boolean in_array (mixed needle, array haystack [, boolean strict]);
Let's take a look at the example below to check whether the variable apple is already in the array. if so, a piece of information is output:

$fruit = "apple";$fruits = array("apple","banana","orange","pear");if( in_array($fruit,$fruits) ) 

Echo "$ fruit is already in the array ";
The third parameter is optional. it forces in_array () to consider the type during search.

Array_key_exists () function

If a specified key is found in an array, the array_key_exists () function returns true; otherwise, false. The format is as follows:
Boolean array_key_exists (mixed key, array );
The following example searches for apple in the array key. if it is found, the color of the fruit is output:

$fruit["apple"] = "red";$fruit["banana"] = "yellow";$fruit["pear"] = "green";if(array_key_exists("apple", $fruit)){ printf("apple's color is %s",$fruit["apple"]);}

The result of executing this code is as follows:

apple's color is red

Array_search () function

The array_search () function searches for a specified value in an array. If yes, the corresponding key is returned. otherwise, false is returned. The format is as follows:

mixed array_search(mixed needle,array haystack[,boolean strict])

The following example searches for a specific date (December 7) in $ fruits. if it is found, information about the corresponding state is returned:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$founded = array_search("green", $fruits);if($founded)  printf("%s was founded on %s.",$founded, $fruits[$founded])

The program running result is as follows:

watermelon was founded on green.

Array_keys () function

The array_keys () function returns an array containing all the keys found in the searched array. The format is as follows:

array array_keys(array array[,mixed search_value])

If the optional parameter search_value is included, only the key that matches the value is returned. The following example outputs all the arrays found in the $ fruit array:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$keys = array_keys($fruits);print_r($keys);

The program running result is as follows:

Array ( [0] => apple [1] => banana [2] => watermelon )

Array_values () function

The array_values () function returns all values in an array and automatically provides a numerical index for the returned array. The format is as follows:

array array_values(array array)

The following example gets the values of each element found in $ fruits:

$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$values = array_values($fruits);print_r($values);

The program running result is as follows:

Array ( [0] => red [1] => yellow [2] => green )

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.