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 variable names and keywords or indexes to access its contents.
Like other variables, using operator = can change the contents of an array element. Array cells can be accessed through the Array[key] syntax.
Basic operations of 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 an 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 strings and numbers, the array begins with an array and is represented by a key value, Print_r output
The results of Boolean and null do not make sense, so use var_dump more appropriately/through loops to display all values in the array for ($i = 0; $i < 5; $i + +) {echo $users [$i];
Echo ' <br/> ';
//through count/sizeof count 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/> '; //You can also iterate through the array through a Foreach loop, which is the benefit of not having to consider the key foreach ($users as $value) {echo $value. ' <br/>//DOT is the string concatenation symbol}//foreach loop traversal $key => $value; $key and $value are variable names, and you can set up a foreach ($users as $key => $value ) {echo $key. <br/>/Output Key}?>
To create an array of custom keys
<?php
//Create a custom key array
$ceo = Arrays (' Apple ' => ' Jobs ', ' Microsoft ' => ' Nadella ', ' Larry Page ', ' Eric ');
If you do not declare the key of an element, it starts at 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 ' => ' foo ',
];
Print_r ($array);//array ([foo] => bar [bar] => foo)
?>
You can use the short array definition syntax from php5.4 and replace array () with []. Somewhat similar to the definition of an array in JavaScript.
Use of each ()
<?php
//To create an array by assigning a value to a group element
$ages [' trigkit4 '] =;
echo $ages. ' <br/>//array
//Because the index of the associated array is not a number, it cannot be traversed through a for loop, only by using the Foreach Loop or the 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 into a new array and assigns it to $a
echo $a [0];//trigkit4
//!! Represents the conversion of real-world 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 the index
list ($var 1, $var 2) with key as Number = $a;
echo $var 1;//boy
?>
Note: List only knows the index with key as number
Ordering of array elements
Reverse Sort: sort (), Asort (), and Ksort () are both forward-sorted and, of course, corresponding reverse-sorted.
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 (), which deletes and returns an element at the end of the array.
Array_rand () returns one or more keys in the array.
function shuffle () sorts the elements of an array randomly.
function Array_reverse () gives a reverse ordering of an original array
use of various APIs for arrays
count () and sizeof () count the number of subscripts in an array
Array_count_values () Statistics the number of subscript values within an array
<?php $numbers = Array (' 100 ', ' 2 '); Sort ($numbers, sort_string),//sorted by string, the string only compares the first bit size print_r ($numbers);//array ([0] => [1] => 2) $arr = Array (
' Trigkit4 ', ' 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 ordering of the original array.
Array ([0] => a [1] => b [2] => C [3] => D [4] => 0 [5] => 1)//array copy $arr 1 = array (' 10 ', 2);
$arr 2 = & $arr 1; $arr 2 [] = 4; $arr 2 was changed, $arr 1 is still Array (' Ten ', 3) Print_r ($arr 2),//array ([0] => [1] => 2 [2] => 4)//asort use $ar
R3 = & $arr 1;/now arr1 and ARR3 are the same $arr 3 [] = ' 3 '; Asort ($arr 3);///array to sort and preserve the original relationship Print_r ($arr 3),//Array ([1] => 2 [2] => 3 [0] =>)//ksort Use $fruits =
Array (' C ' => ' banana ', ' a ' => ' apple ', ' d ' => ' orange ');
Ksort ($fruits); Print_r ($fruits);//arraY ([a] => Apple [c] => Banana [d] => Orange)//unshift uses Array_unshift ($array, ' z '); add an element Print_r at the beginning ($ Array);//array ([0] => Z [1] => a [2] => b [3] => C [4] => D [5] => 0 [6] => 1)//current (POS) Use echo current ($array);//z; Gets the use of the Echo next ($array) for the active cell//next in the current array;//a the internal pointer in the array forward one//reset using echo reset
($array);//z the array internal pointer to the first cell//prev the use of Echo next ($array); 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);//statistic the number of occurrences of an array element Print_r (Array_count_values ($num));//array ([A] => 3 [1] => 2 [a] => [1] =
> 1 [0] => 1)?>
Current (): Each array has an internal pointer to his active 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" (?>) ".
An instance of an array
the use of Array_pad functions
<?php
//array_pad function, array array and end-selective append
$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 fill to the right and negative numbers fill to the left.
Use of unset ()
the use of <?php//unset () is
$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 (1,2, $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 an array member
<?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 Delete duplicate values from the 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 the 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 values
<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 No value red ";
} else{
echo "exists numeric $result";//There is a value of 0
}