Array (cont.)
Multidimensional Arrays:
<?php $products = array(array('TIR','tires',100),
array('OIL','oil',10),
array('SPK','Spark Plugs',4));
for($i=0;$i<3;$i++){
for($k=0;$k<3;$k++){
echo $products[$i][$k].' | ';
}
echo "\n";
}
?>
Run results are shown:
The above code defines a two-dimensional array, the array may be defined by nesting more dimensions, similar to the above method.
Sorting an array
Start with the one-dimensional looks:
<?php
$products = array('Banana','banana','Apple','apple','Pear','pear',4,8,10);
sort($products,SORT_REGULAR);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
sort($products,SORT_NUMERIC);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
sort($products,SORT_STRING);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
sort($products,SORT_LOCALE_STRING);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
sort($products,SORT_NATURAL);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
sort($products,SORT_FLAG_CASE & SORT_STRING);
for($i=0;$i<9;$i++){
echo $products[$i].'|';
}
echo "\n";
?>
Run results shown in Figure:
Here we call the sort () function to sort, the second parameter is optional, there are these six, the second argument by default (ie a sort function) all uppercase letters will front lowercase, when specified sorting type , there are many places to be noted that, for example: numbers smaller than 12, but the string '2' but greater than '12'. SORT_LOCALE_STRING expressed as a string array is ordered according to the current system locale. SORT_NATURAL sort order sort of natural, like combinations to sort strings and numbers.
asort (): If the key-value pairs, it by value in ascending order .
ksort (): If the key-value pair will keys in ascending order .
rsort (): and sort () On the contrary, it will be sorted in descending order.
arsort (): and asort () Instead it by value in descending order .
krsort (): and ksort () Instead, it keys in descending order .