PHP has a traditional array structure, and as the version of the upgrade is constantly improving, such as from php5.4 can be used to define the short array syntax, we will talk about in the text, the following look at PHP commonly used array operation method Note collation:
Overview to access the contents of a variable, you can use its name directly. If the variable is an array, you can use a combination of the variable name and keyword or index to access its contents.
Like other variables, you can change the contents of an array element by using operator =. Array cells can be accessed through the Array[key] syntax.
Basic Operations for arrays
PHP definition array:
<?php $array = Array (); $array ["key"] = "values";?>
There are two main ways to declare an array in PHP:
1. Declare an array with the array () function,
2. Assign a value directly to the array element.
<?php//array array $users = array (' phone ', ' computer ', ' dos ', ' Linux '); echo $users;//Only print out data type Array print_r ($users);//array ([0] = phone [1] = computer [2] = = DOS [3] = Linux) $numbers = range (1,5);//Create an array containing the specified range Print_r ($numbers);//array ([0] = 1 [1] = 2 [2] = 3 [3] + = 4 [4] = > 5) Print_r (true),//1 Var_dump (false),//bool (false)//print_r can simply print the string and the number, the array begins with an array and is represented by the key value, Print_ R output Boolean and NULL results have no meaning, so it is more appropriate to use Var_dump//loop to display all the values in the array for ($i = 0; $i < 5; $i + +) {echo $users [$i]; Echo ' <br/> '; }//by count/sizeof the number of cells in the array or the number of attributes in the object for ($i = 0; $i < count ($users); $i + +) {echo $users [$i]; Echo ' <br/> '; }//can also iterate through the array through the Foreach loop, which has the advantage of not having to consider key foreach ($users as $value) {echo $value. <br/> ';//dot number is a string join symbol}//foreach loop traversal $key = $value; $key and $value are variable names, you can set the foreach ($users as $key + $value) { echo $key. ' <br/> ';//output key}?>
Creating an array of custom keys
<?php //Create an array of custom keys $ceo = Array (' apple ' = ' jobs ', ' microsoft ' = ' Nadella ', ' Larry Page ', ' Eric '); If you do not declare the key of the element, it will start from zero print_r ($CEO);//array ([Apple] = jobs [Microsoft] = Nadella [0] = Larry Page [1] =& Gt Eric) echo $ceo [' Apple '];//jobs //php5.4 usage $array = [ "foo" + "Bar", "bar" and "foo", ]; Print_r ($array);//array ([foo] = bar [bar] = foo)?>
From php5.4 you can use the short array definition syntax and replace array () with []. A bit similar to the definition of arrays in JavaScript.
Use of each ()
<?php //Create an array $ages [' trigkit4 '] = by assigning a value to a set of elements; echo $ages. ' <br/> ';//array //Because the index of the associated array is not a number, it cannot be traversed by a for loop, only through the use of the Foreach Loop or list () and each () structure//each/ / Each returns the current key/value pair in the array and moves the array pointer forward one step $users = Array (' TRIGKIT4 ' =>22, ' Mike ' =>20, ' John ' =>30); Print_r (each ($users));//array ([1] = [value] = [0] = = TRIGKIT4 [key] = TRIGKIT4) //equivalent: $a = a Rray ([0]=>trigkit4,[1]=>22,[value]=>22,[key]=>trigkit4); $a = each ($users);//each the first element of the original array and wraps it into a new array and assigns a value to $ A echo $a [0];//trigkit4 //!! Represents the conversion of real data to a Boolean value Echo!! Each ($users);//1?>
Each pointer points to the first key-value pair and returns the first array element, gets its key-value pair, and wraps it into a new array
list () uses the list to assign the value of the array to some variables, see the following example:
<?php $a = [' 2 ', ' abc ', ' Def ']; List ($var 1, $var 2) = $a; echo $var 1. ' <br/> ';//2 echo $var 2;//abc $a = [' name ' = ' trigkit4 ', ' age ' =>22, ' 0 ' + ' boy ']; List only recognizes key as the index of the number list ($var 1, $var 2) = $a; Echo $var 1;//boy?>
Note: List only knows the index of key as a number
the sorting of the array elements is reversed: sort (), Asort (), and Ksort () are both positive and, of course, the corresponding reverse sort.
Implement reverse: Rsort (), Arsort (), and Krsort ().
The Array_unshift () function adds a new element to the array header, and the Array_push () function adds each new element to the end of the array.
Array_shift () deletes the first element of the array header, and its inverse function is array_pop (), deleting and returning an element at the end of the array.
Array_rand () returns one or more keys in the array.
The function shuffle () randomly sorts the elements of an array.
The function Array_reverse () gives a reverse sort of the original array
array of various APIs using the Count () and sizeof () statistics array subscript number
Array_count_values () counts the number of subscript values in an array
<?php $numbers = Array (' 100 ', ' 2 '); Sort ($numbers, sort_string);//Sort by string, string only compares first bit size print_r ($numbers);//array ([0] = [1] = 2) $arr = Array (' tr Igkit4 ', ' banner ', ' 10 '); Sort ($arr, sort_string); Print_r ($arr);//array ([0] = [1] = banner [2] = = TRIGKIT4) shuffle ($arr); Print_r ($arr);//random sort $array = Array (' A ', ' B ', ' C ', ' d ', ' 0 ', ' 1 '); Array_reverse ($array); Print_r ($array);//reverse order of the original array. Array ([0] = a [1] = b [2] = = c [3] = d [4] = 0 [5] + 1)//copy of the array $arr 1 = array (' 10 ', 2); $arr 2 = & $arr 1; $arr 2 [] = 4; $arr 2 were changed, $arr 1 was still Array ('//array ', 3) Print_r ($arr 2), and the//asort ([0] = [1] = 2 [2] + 4) use $arr 3 = & $arr 1;//Now ARR1 and ARR3 are the same $arr 3 [] = ' 3 '; Asort ($arr 3);//sorting the array and preserving the original relationship Print_r ($arr 3);//Array ([1] = 2 [2] = 3 [0] = +)//ksort Use $fruits = arr Ay (' c ' = ' banana ', ' a ' = ' = ' apple ', ' d ' = ' orange '); Ksort ($fruits); Print_r ($fruits);//array ([A] = Apple [c] = banAna [d] = orange)//unshift use Array_unshift ($array, ' z ');//Add an element Print_r ($array) at the beginning,//array ([0] = Z [1] => ; A [2] = b [3] = c [4] = + d [5] = 0 [6] = 1)//current (POS) using echo current ($array);//z; Gets the active cell in the current array Next uses echo next ($array);//a the inner pointer in the array moves forward one//reset using echo reset ($array);//z; Pointing the array interior pointer to the first cell//prev using echo NEX T ($array);//a; Echo prev ($array);//z; Returns a//sizeof using echo sizeof ($array),//7; counts the number of array elements//array_count_values $num = Array (10,20,30,10 , 20,1,0,10);//Count the number of occurrences of an array element Print_r (Array_count_values ($num));//array ([Ten] = 3 [] = 2 [+] = 1 [1] = 1 [0] = 1)?>
Current (): Each array has an internal pointer to his present cell, initially pointing to the first element inserted into the array
For loop traversal
<?php $value = range (0,120,10); for ($i =0; $i <count ($value), $i + +) { print_r ($value [$i]. ' ');//0-ten-A- }?>
Use of An instance of an array array_pad function
<?php //array_pad function, array array is selectively appended $num = Array (1=>10,2=>20,3=>30); $num = Array_pad ($num, 4,40); Print_r ($num);//array ([0] = [1] = [2] = [3] = +) $num = Array_pad ($num, -5,50);//array_pa D (array,size,value) print_r ($num);//array ([0] = [1] = [2] = [3] = [4] = +)?>
Size: The specified length. Integers are filled to the right, and negative numbers are filled to the left.
Use of unset ()
use of <?php//unset () $num = Array_fill (0,5,rand (1,10)),//rand (Min,max) print_r ($num);//array ([0] = > 8 [1] = 8 [2] = 8 [3] = 8 [4] + 8) echo ' <br/> '; Unset ($num [3]); Print_r ($num);//array ([0] = 8 [1] = 8 [2] = 8 [4] = 8)?>
Use of Array_fill ()
use of <?php//array_fill () $num = Range (' A ', ' e '); $arrayFilled = Array_fill ($num);//array_fill (start,number,value) echo ' <pre> '; Print_r ($arrayFilled);? >
Use of Array_combine ()
<? PHP $number = Array (1,2,3,4,5); $array = Array ("I", "Am", "A", "PHP", "er"); $newArray = Array_combine ($number, $array); Print_r ($newArray);//array ([1] = I [2] + Am [3] = = A [4] + = PHP [5] = er)?>
Array_splice () Delete array members
<?php $color = Array ("Red", "green", "blue", "yellow"); Count ($color); Get 4 Array_splice ($color, 1, 1);//delete the second element Print_r (count ($color));//3 echo $color [2];//yellow echo $color [1]; Blue?>
Array_unique to delete duplicate values in an array
<?php $color =array ("Red", "green", "blue", "yellow", "blue", "green"); $result = Array_unique ($color); Print_r ($result);//array ([0] = red [1] = green [2] = blue [3] = yellow)?>
Array_flip () Exchange the key values and values of an array
<? PHP $array = Array ("Red", "Blue", "Red", "Black"); Print_r ($array); echo "<br/>"; $array = Array_flip ($array);// Print_r ($array);//array ([Red] = 2 [Blue] = 1 [Black] = 3)?>
Array_search () Search value
<meta charset= "Utf-8" ><?php $array = Array ("Red", "Blue", "Red", "Black"); $result =array_search ("Red", $array)//array_search (value,array,strict) if (($result = = = NULL)) { echo ' There is no value red "; } else{ echo "presence value $result";//Presence value 0 }?>
Summary: The above is the entire content of this article, I hope to be able to help you learn.