Example 7: Array value basic operation
Copy CodeThe code is as follows:
$arr =array (' a ' + = "You", ' B ' = "I", "he");
$arr []= "Other";
echo $arr [' B ']. "
";
$arr [' C ']= "";//Give a null value, but still occupy the position of the
echo Count ($arr). "
";//array has the number of values.
unset ($arr [' B ']);//This function can unregister a string, an entire array of equivalent types, and a reference type.
Print_r ($arr);//This function prints the entire internal structure of a value, reference type.
echo "
";
foreach ($arr as $key = $value)
echo $key. ":". $value. "
";//Loop output the value of the entire array.
?>
Example 8: Converting between arrays and strings
Copy CodeThe code is as follows:
$arr =array (' a ' + = "You", ' B ' = "I", "he");
echo $arr =implode ('-', $arr);//array to string, connector-
echo "
";
Print_r (Explode ('-', $arr, 2));//String to array. If the last parameter is not used, indicates that all '-' is split into an array
?>
Example 9: Array sorting
Copy CodeThe code is as follows:
$arr =array (' b ' = ' You ', ' a ' = ' I ', ' he ');
Ksort ($arr);//array key values are sorted by pinyin (UTF-8 encoding), and 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 "
";
Asort ($arr);//The array is sorted by the Pinyin (UTF-8 encoding) of the values, and the key values are not lost. If you do not have a key value, you can use the function sort (), or if the reverse order 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 "
";
$arr =array (10000,100,1000);
Natsort ($arr);//Sort values by number, while Natcasesort () is case insensitive
Print_r ($arr);
echo "
";
Print_r (Array_reverse ($arr));//Array reverse order
echo "
";
?>
Example 10: arrays, digital random extraction, digital and encoding conversions
Copy CodeThe code is as follows:
$arr =array (' b ' = ' You ', ' a ' = ' I ', ' he ');
$key =array_rand ($arr, 2);//array randomly extracts 2 key values, returning an indexed array with two key values
echo $arr [$key [0]]. $arr [$key [1]];
echo "
";
echo Mt_rand (60,100);//returns a random integer within the range.
echo "
";
Echo Chr (Ord (' A '), Ord (' Z ')));//number and encoding conversion mt_rand.
echo "
";
?>
The function of the array is finished, I only choose a few representative let everyone get started, in fact, there are some not commonly used array functions. In addition, we can use a for or a foreach loop to work with arrays, to generate their own my_ functions, stupid and stupid method.
http://www.bkjia.com/PHPjc/322479.html www.bkjia.com true http://www.bkjia.com/PHPjc/322479.html techarticle Example 7: Array value Basic operations copy code code as follows: PHP $arr =array (' a ' = "You", ' B ' = "I", "he"), $arr []= "other"; Echo $arr [' B ']. " BR "; $arr [' C ']= "";//Give a null value, but still occupy ...