PHP definition Array
<?php $array = []; $array ["key"] = "value"; ? >
There are two main ways to declare an array in PHP:
Declare an array with the array () function.
2. Assign a value directly to the array element.
<?php //array array $users = [' A ', ' B ', ' C ', ' d ']; echo $users;//Only print out data type Arra print_r ($users);//array ([0] = a[1] [b[2] = c[3] + d) $numbers = Ran GE (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 will begin with an array and is represented as a key, Print_ R output Boolean and NULL results are meaningless, so it's more appropriate to use Var_dump
To display all the values in the array through a loop
for ($i = 0; $i < 5; $i + +) { echo $users [$i]; Echo ' <br/> '; }
Count the number of cells in an array or the number of attributes in an object by count/sizeof
for ($i = 0; $i < count ($users); $i + +) { echo $users [$i]; Echo ' <br/> '; }
You can also iterate through the array through the Foreach loop, which is the benefit 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 a custom array $array = ["A" =>1, "B" =>2, "C" =>3, "D", "E"]; If there is no declaration key, it will start from zero print_r ($array);//array ([A]=>1,[b]=>2,[c]=>3,[0]=>d,[1]=>e); >
each () use
<?php//Create an array $array [' key '] = 22 by assigning a value to a set of elements; echo $array. ' <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//EAC H returns the current key/value pair in the array and moves the array pointer forward one step $users = Array (' key ' =>22, ' key1 ' =>20, ' Key2 ' =>30); Print_r (each ($users));//array ([1] = [value] = [0] = = Array[key] = array)//equivalent to: $a = array ( [0]=>array,[1]=>22,[value]=>22,[key]=>array]; $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];//array//!! 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
use of list ()
The list is used to assign the values of the array to some variables, as shown in 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
ordering of array elements
The reverse sort: 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
Use of various types of APIs for arrays
Note: count () and sizeof () count the number of array subscripts
Array_count_values () counts the number of subscript values in an array
<?php $numbers = Array (' + ', ' 2 '); Sort ($numbers, sort_string);//Sort by string, string only compares first bit size print_r ($numbers);//array ([0] = [1] = 2) $arr = Array (' TRIGKIT4 ', ' banner ', ' Ten '); 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 array
$arr 1 = Array (' Ten ', 2); $arr 2 = & $arr 1; $arr 2 [] = 4; $arr 2 were changed, $arr 1 is still an Array (' 3 ',//array), Print_r ($arr 2), and ([0] = [1] = 2 [2] = 4)
Use of Asort
$arr 3 = & $arr 1;//Now ARR1 and ARR3 are the same $arr 3 [] = ' 3 '; Asort ($arr 3);//Sort the array and retain the original relationship Print_r ($arr 3);//Array ([1] = 2 [2] = 3 [0] + 10)
Use of Ksort
$fruits = Array (' c ' = = ' banana ', ' a ' = = ' Apple ', ' d ' = ' orange '); Ksort ($fruits); Print_r ($fruits);//array ([A] = Apple [c] = banana [d] = orange)
Use of Unshift
Array_unshift ($array, ' z ');//Add an element Print_r ($array) at the beginning,//array ([0] = Z [1] = a [2] = = b [3] = c [4] =& Gt d [5] = 0 [6] = 1)
Use of current (POS)
Echo current ($array);//z; Gets the active cell in the current array
Use of Next
Echo Next ($array); Move the inner pointer in the array forward//a one
Use of Reset
echo Reset ($array);//z the inner pointer of the array to the first cell
Use of Prev
Echo Next ($array);//a; Echo prev ($array);//z; pour back a
sizeof uses echo sizeof ($array);//7; counts the number of array elements //array_count_values $num = Array (10,20,30,10,20,1,0,10 )///statistics array element occurrences print_r (array_count_values ($num));//array ([Ten] = 3 [] = 2 [+] = 1 [1] = 1 [0] =&G T 1)?>
Urrent (): Each array has an internal pointer to his current 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- }?>
An instance of an array
Use of the 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_pad (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 } ?>