Some common functions of adding, deleting and inserting operations of the logarithmic group in PHP _php skills

Source: Internet
Author: User
Tags pear

Sometimes we need to extend an array, or delete part of the array, and PHP provides some functions for expanding and shrinking the array. These functions can be handy for programmers who want to emulate a variety of queue implementations (FIFO, LIFO). As the name suggests, the function names of these functions (push, pop, shift, and unshift) clearly reflect their role.

PS: The traditional queue is a data structure, delete elements and add the same order of elements, called advanced First Out, or FIFO. Instead, stacks are another type of data structure in which the order in which elements are deleted is in contrast to the order in which they were added, which becomes LIFO, or LIFO.

Add an element to the array header

The Array_unshift () function adds an element to the array header. All of your numeric keys are modified accordingly to reflect their new position in the array, but the association keys are unaffected. The form is as follows:

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

The following example adds two kinds of fruit to the front of the $fruits array:

$fruits = Array ("Apple", "banana");
Array_unshift ($fruits, "orange", "pear")
//$fruits = Array ("Orange", "pear", "apple", "banana");

Add an element to the end of an array

The return value of the Array_push () function is int, is the number of elements in the array after the data is pressed, you can pass multiple variables as arguments for this function, and press multiple variables into the array. In the form of:

(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")

To delete a value from an array header

The Array_shift () function deletes and returns the elements found in the array. As a result, if you are using a numeric health, all corresponding values are moved down, and the array using the association keys is unaffected. In the form of:

Mixed Array_shift (array array)

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

$fruits = Array ("Apple", "banana", "orange", "pear");
$fruit = Array_shift ($fruits);
$fruits = Array ("Banana", "orange", "pear")
//$fruit = "Apple";

Delete an element from the end of an array

The Array_pop () function deletes and returns the last element of the array. In the form of:

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";

Finding, filtering, and searching array elements are some of the common features of array operations. Here are a few related functions.

In_array () function

The In_array () function searches for a specific value in an array rollup and returns False if the value is found to return true. The form is as follows:
Boolean In_array (mixed Needle,array Haystack[,boolean strict]);
Look at the example below to find out whether the variable Apple is already in the array, and if it is, output a piece of information:

$fruit = "Apple";
$fruits = Array ("Apple", "banana", "orange", "pear");
if (In_array ($fruit, $fruits)) 

echo "$fruit already in array";
The third argument is optional, which forces In_array () to consider the type when searching.

Array_key_exists () function

If a specified key is found in an array, the function array_key_exists () returns TRUE, otherwise it returns false. The form is as follows:
Boolean array_key_exists (mixed Key,array array);
The following example searches for Apple in the array key and, if found, prints the color of the fruit:

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

Execute this code to get the result:

Apple ' s color is red

Array_search () function

The Array_search () function searches an array for a specified value and returns False if it finds the corresponding key. The form is as follows:

Mixed Array_search (mixed Needle,array Haystack[,boolean Strict])

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

$fruits ["apple"] = "red";
$fruits ["banana"] = "yellow";
$fruits ["Watermelon"]= "green";
$founded = Array_search ("green", $fruits);
if ($founded) 
 printf ("%s is founded on%s.", $founded, $fruits [$founded])

The results of the program operation are as follows:

Watermelon is founded on green.

Array_keys () function

The Array_keys () function returns an array that contains all the keys found in the searched array. The form is as follows:

Array Array_keys (array array[,mixed search_value])

If you include an optional parameter search_value, 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 results of the program operation are as follows:

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

Array_values () function

The Array_values () function returns all the values in an array and automatically provides a numeric index for the returned array. The form is as follows:

Array array_values (array array)

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

$fruits ["apple"] = "red";
$fruits ["banana"] = "yellow";
$fruits ["Watermelon"]= "green";
$values = Array_values ($fruits);
Print_r ($values);

The results of the program operation are 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.