Instance 7: Array value basic operations
Copy CodeThe code is as follows:
<?php
$arr =array (' A ' => ' you ', ' B ' => ' me ', ' he ');
$arr []= "Other";
echo $arr [' B ']. <br> ";
$arr [' C ']= '];/to a null value, but still occupy the position of
echo Count ($arr). <br>;//The number of values in the array.
unset ($arr [' B ']);//This function can unregister the string, the entire array equivalence type, and the reference type.
Print_r ($arr);//This function prints the entire internal structure of a numeric value, reference type.
echo "<br>";
foreach ($arr as $key => $value)
echo $key. ":". $value. " <br>/Loops Output The value of the entire array.
?>
Instance 8: Converting between arrays and strings
Copy CodeThe code is as follows:
<?php
$arr =array (' A ' => ' you ', ' B ' => ' me ', ' he ');
echo $arr =implode ('-', $arr);//array-turn string, connector-
echo "<br>";
Print_r (Explode ('-', $arr, 2));//String to array. If the last parameter is not used, it means that all '-' are split into groups
?>
Instance 9: Array Ordering
Copy CodeThe code is as follows:
<?php
$arr =array (' B ' => ' You ', ' a ' => ' me ', ' he ');
Ksort ($arr);////array key value Pinyin (UTF-8 encoding) sorted, key values are not lost. Note that this sort does not return the new array, but instead directly passes the original array as a reference.
Print_r ($arr);
echo "<br>";
Asort ($arr);//arrays are sorted by value Pinyin (UTF-8 encoding) and key values are not lost. If you do not have a key value, you can use the function sort () and if the reverse sequence also has a function rsort (). Note that this sort does not return the new array, but instead directly passes the original array as a reference.
Print_r ($arr);
echo "<br>";
$arr =array (10000,100,1000);
Natsort ($arr);//To naturally sort values by number, while Natcasesort () is case-insensitive
Print_r ($arr);
echo "<br>";
Print_r (Array_reverse ($arr));//Array reverse order
echo "<br>";
?>
Example 10: Array, digital random extraction, digital and coded conversions
Copy CodeThe code is as follows:
<?php
$arr =array (' B ' => ' You ', ' a ' => ' me ', ' he ');
$key =array_rand ($arr, 2);//array randomly extracts 2 key values and returns an array of two key values
echo $arr [$key [0]]. $arr [$key [1]];
echo "<br>";
echo Mt_rand (60,100);//returns a random integer within the range.
echo "<br>";
Echo Chr (Mt_rand (Ord (' A '), Ord (' Z '));/number and encoding conversion.
echo "<br>";
?>
The function of the array is finished, I only pick a few representative to let you get started, in fact, there are some less commonly used array functions. In addition, we can use a for or a foreach loop to process the array, generate their own my_ function, stupid method.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.