Summary of some commonly used add, delete, and insert operation functions for arrays in PHP_php skills

WBOY
Release: 2016-05-16 20:04:00
Original
1009 people have browsed it

Sometimes we need to expand an array or delete a part of the array. PHP provides some functions for expanding and shrinking arrays. These functions provide convenience for programmers who wish to emulate various queue implementations (FIFO, LIFO). As the name suggests, the function names of these functions (push, pop, shift, and unshift) clearly reflect their functions.

PS: The traditional queue is a data structure. The order of deleting elements and adding elements is the same, which is called first-in-first-out, or FIFO. In contrast, a stack is another data structure in which elements are removed in the reverse order in which they were added. This becomes last-in-first-out, or LIFO.

Add elements to the head of the array

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

int array_unshift(array array,mixed variable[,mixed variable])
Copy after login

The following example adds two fruits in front of the $fruits array:

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

Add elements to the end of the array

The return value of the array_push() function is of type int, which is the number of elements in the array after pushing the data. You can pass multiple variables as parameters to this function and push multiple variables into the array at the same time. Its form is:

(array array,mixed variable [,mixed variable...])
Copy after login

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")
Copy after login

Delete value from array head

The array_shift() function removes and returns the element found in the array. The result is that if numeric keys are used, all corresponding values ​​are shifted down, while arrays using associative keys are not affected. Its form is:

mixed array_shift(array array)
Copy after login

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";
Copy after login

Delete elements from the end of the array

The array_pop() function removes and returns the last element of the array. Its form is:

mixed array_pop(aray target_array);
Copy after login

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

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

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

in_array() function

The in_array() function searches for a specific value in an array and returns true if the value is found, otherwise it returns false. Its form is as follows:
boolean in_array(mixed needle,array haystack[,boolean strict]);
Let’s look at the following example to find whether the variable apple is already in the array. If it is, output a piece of information:

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

echo "$fruit is already in the array";
The third argument is optional and forces in_array() to consider types 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. Its form is as follows:
boolean array_key_exists(mixed key,array array);
The following example will search for apple in the array key, and if found, will output 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"]);
}
Copy after login

The result of executing this code:

apple's color is red
Copy after login

array_search() function

The array_search() function searches for a specified value in an array and returns the corresponding key if found, otherwise it returns false. Its form is as follows:

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

The following example searches $fruits for a specific date (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 was founded on %s.",$founded, $fruits[$founded])
Copy after login

The results of running the program are as follows:

watermelon was founded on green.
Copy after login

array_keys() function

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

array array_keys(array array[,mixed search_value])
Copy after login

If the optional parameter search_value is included, only keys matching that value will be returned. The following example will output all arrays found in the $fruit array:

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

The results of running the program are as follows:

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

array_values() function

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

array array_values(array array)
Copy after login

The following example will get the value of each element found in $fruits:

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

The results of running the program are as follows:

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template