about using and explaining the array functions of PHP
First, I'm going to write out some of the array functions I've used.
①.in_array
My own sentiment: In_array, the first argument is the string or number you want to find, and then the second is an array, and then if you have the value of the first parameter in this array, return to ture, otherwise it will return false.
definition and Usage:
The In_array () function searches the array for the given value
Grammar
in_array(value,array,type)
Parameters |
Describe |
Value |
Necessary. Specifies the value to search for in the array. |
Array |
Necessary. Specifies the array to search for. |
Type |
Optional. If set to true, checks whether the searched data is the same as the type of the array's value. |
Description
Returns true if the given value exists in an array of arrays. If the third argument is set to True, the function returns true only if the element exists in the array and the data type is the same as the given value.
If no arguments are found in the array, the function returns FALSE.
Note: If the value parameter is a string and the type parameter is set to True, the search is case-sensitive.
Example 1
$people = array("Peter", "Joe", "Glenn", "Cleveland");if (in_array("Glenn",$people)) { echo "Match found"; }else { echo "Match not found"; }?>
Output:
Match found
Example 2
$people=Array("Peter","Joe","Glenn","Cleveland", at);if(In_array ("All",$people,TRUE)) {Echo "Match found
"; }Else{Echo "Match not Found
"; }if(In_array ("Glenn",$people,TRUE)) {Echo "Match found
"; }Else{Echo "Match not Found
"; }if(In_array ( at,$people,TRUE)) {Echo "Match found
"; }Else{Echo "Match not Found
"; }?>
Output:
Matchnot foundMatch foundMatch found
②array_splice
My own view, this method is mainly to insert a value in the array to the specified position
definition and Usage:
The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return, but instead deletes them and replaces them with other values.
If the fourth argument is supplied, the previously selected elements will be replaced by the array specified by the fourth parameter.
The last generated array will be returned.
Grammar
array_splice(array,offset,length,array)
Parameters |
Describe |
Array |
Necessary. Specifies the array. |
Offset |
Necessary. Numerical. If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, it is removed from the offset specified by the value of the countdown at the end of the input array ... |
Length |
Optional. Numerical. If this argument is omitted, all parts of the array from offset to end are moved. If length is specified and positive, then so many elements are removed. If length is specified with a negative value, all elements in the middle of offset to the end of the array ending with length are removed. |
Array |
The removed element is substituted by the elements in this array. If no value is removed, the elements in this array are inserted into the specified position. |
Hints and Notes
Tip: If the function does not delete any elements (length=0), the alternate array is inserted from the position of the start parameter. (see example 3)
NOTE: The keys in the alternate array are not preserved.
Example 1
!--? php $a 1 =< Span class= "Hljs-keyword" >array (0 => "Dog" , 1 => "Cat" , 2 => "Horse" , 3 => "Bird" ); $a 2 =array (0 =>, 1 = "Lion" ); Array_splice ( $a 1 , 0 , 2 , $a 2 );p Rint_r ( $a 1 ); ?>
Output:
Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
Example 2
!--? php $a 1 =array (0 => "Dog" , 1 = "Cat" , 2 => Horse ", 3 =>" Bird "); $a 2 =array (0 =>, 1 = "Lion" ); Print_r (Array_splice ( $a 1 , 0 , 2 , $a 2 )); ?>
Output:
Array ( [0] => Dog [1] => Cat )
Example 3
The length parameter is set to 0:
$a1=array(0=>"Dog",1=>"Cat");$a2=array(0=>"Tiger",1=>"Lion");array_splice($a1,1,0,$a2);print_r($a1);?>
Output:
Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )
Subsequent updates are also: Array_unshift
Array_push, etc.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.