Home > Article > Backend Development > php array
1 Create array in php
$product = array('tires','oil','spark');//array() is actually a language structure rather than a function
2 If you need to create an ascending arrangement The numbers are stored in an array. You can use the range() function to automatically create the array
$numbers=range(1,10)//1 2 3 4 5 6 7 8 9 10
$numbers=range(1,10,2 )//1 3 5 7 9 The third parameter sets the step between parameters
range syntax:
range(first,second,step)//Create and return an array containing elements in the specified range
first: required. Specifies the minimum value of an array element.
second:required. Specifies the maximum value of an array element.
step: optional. Specifies the stepping between elements. The default is 1.
3 Use loop to access array
for loop to access array
for($i=0;i echo $product[i]." ";
}
foreach to access array
foreach($product as $pro){
echo $pro."";
}
4 Associative array
$prices= array('tires'=>100,'oil'=>200,'spark'=>30);
Access array$ prices['tires'] ,$prices['oil'],$prices['spark']
foreach access array
foreach ($price as $key =>$value){
echo $key.'-'.$ value.'
';
}
each() structure prints array
while($element = each($price)){
echo $element['key'].'-'.$element['value']. '
';
}
list() and each() print array
while(list($product,$pri)=each(prices)){
echo "$product - $pri
";
}
each( ) Syntax
each(array)// The function generates an array consisting of the key name and key value of the element pointed to by the current internal pointer of the array, and moves the internal pointer forward. The returned array contains four elements: key 0, 1, key and value. Cells 0 and key contain the key names of the array cells, and 1 and value contain the data.
If the internal pointer exceeds the range of the array, this function will return FALSE.
array: required. Specify the array to be used
list() syntax:
list(var1,var2...)//Assign a value to a set of variables using elements in the array. List is actually a structure, not a function, similar to array
var1 is required. The first variable to be assigned a value.
var2 optional. There can be multiple variables.
5 multi-dimensional array
$products=array(
array('Tire','tires','100'),
array('Oil','oil','300'),
array('Spak',' Speak','100'),
)
Loop multi-dimensional array
foreach($products as $value){
foreach($value as $va){
echo $va.'';
}
}
6 array Sort
sort(array,sorttype)//Sort the values of the given array in ascending order
array: required. The input array.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Handle values as numbers
•SORT_STRING - Handle values as strings
•SORT_LOCALE_STRING - Handle values as strings, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r ($my_array);
?>
Output:
Array
(
[0] => Cat
[1] => Dog
[2] => Horse
)
asort(array,sorttype) //Perform on the array Sort and maintain index relationships. Mainly used to sort associative arrays where the order of units is important
Array: Required. The input array.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Treat values as numbers
•SORT_STRING - Handle values as strings
•SORT_LOCALE_STRING - Handle values as strings, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
asort($my_array);
print_r($my_array);
?>
Output:
Array
(
[b] => Cat
[a] => Dog
[c] => Horse
)
ksort(array,sorttype)//according to key Sort the array by name, retaining the original keys for the array values
Array: Required. Specifies the array to be sorted.
sorttype: optional. Specifies how to arrange the values of an array. Possible values:
•SORT_REGULAR - Default. Processed with their original type (without changing type).
•SORT_NUMERIC - Handle the value as a number
•SORT_STRING - Handle the value as a string
•SORT_LOCALE_STRING - Handle the value as a string, based on local settings*.
Example:
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
ksort($my_array);
print_r ($my_array);
?>
Output:
Array
(
[a] => Dog
[b] => Cat
[c] => Horse
)
Reverse sort : rsort(),arsort() ,krsort()//For reverse sorting (descending order)
usort(array, sorttype)//Use user-defined function to sort the array.
array: required. Specifies the array to be sorted.
sorttype: required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments for comparison, and work in a manner similar to the following:
•If a = b, return 0
•If a > b, Return 1
•If a Example:
function my_sort($a, $b)
{
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$arr = array("Peter", "glenn","Cleveland","peter","cleveland", "Glenn");
usort($arr, "my_sort");
print_r ($arr);
?>
Output result:
Array
(
[0] => peter
[1] => glenn
[2] => cleveland
[3] = > Peter
[4] => Glenn
[5] => Cleveland
)