PHP programming the fastest to understand the third lecture: PHP array
- 2020-03-31 21:20:13
- OfStack
Example 7: array value basic operation
Example 8: converting between arrays and strings
Instance 9: array sort
Example 10: array, random number extraction, number and code conversion
So that's the end of the list of array functions, and I'm just going to pick a few that are representative to get you started, but in fact, there are some array functions that you don't use very often. In addition, we can use a for or a foreach loop to manipulate the array, generating our own my_ function.
<?php
$arr=array('a'=>" you ",'b'=>" I "," he ");
$arr[]=" other ";
echo $arr['b']."<br>";
$arr['c']="";//I give you a null value, but it still takes up space
echo count($arr)."<br>";//How many values does the array have?
unset($arr['b']);//This function can cancel the string, the entire array equivalent type, and the reference type.
print_r($arr);//This function prints the entire internal structure of the value, reference type.
echo "<br>";
foreach($arr as $key=>$value)
echo $key.":".$value."<br>";//The loop outputs the value of the entire array.
?>
Example 8: converting between arrays and strings
<?php
$arr=array('a'=>" you ",'b'=>" I "," he ");
echo $arr=implode('-',$arr);//Array to string, concatenation -
echo "<br>";
print_r(explode('-',$arr,2));//String to array. If the last parameter is not used, all the '-' is split into an array
?>
Instance 9: array sort
<?php
$arr=array('b'=>" you ",'a'=>" I "," he ");
ksort($arr);//Array key values in pinyin (utf-8 encoding) sort, the key values are not lost. Note that this sort does not return a new array but simply passes the original array as a reference.
print_r($arr);
echo "<br>";
asort($arr);//Arrays are sorted by the pinyin (utf-8 encoding) of the values, and the key values are not lost. If you don't want a key, you can use the function sort(); If the reverse order also has a function rsort(). Note that this sort does not return a new array but simply passes the original array as a reference.
print_r($arr);
echo "<br>";
$arr=array(10000,100,1000);
natsort($arr);//Values are sorted naturally by number, while natcasesort() is case-insensitive
print_r($arr);
echo "<br>";
print_r(array_reverse($arr));//Antitone array
echo "<br>";
?>
Example 10: array, random number extraction, number and code conversion
<?php
$arr=array('b'=>" you ",'a'=>" I "," he ");
$key=array_rand($arr,2);//The array selects two key values at random and returns an indexed array with two key values
echo $arr[$key[0]].$arr[$key[1]];
echo "<br>";
echo mt_rand(60,100);//Returns a random integer in that range.
echo "<br>";
echo chr(mt_rand(ord('a'),ord('z')));//Number and code conversion.
echo "<br>";
?>
So that's the end of the list of array functions, and I'm just going to pick a few that are representative to get you started, but in fact, there are some array functions that you don't use very often. In addition, we can use a for or a foreach loop to manipulate the array, generating our own my_ function.