PHP programming: php array. Example 7: The code for copying the basic operation of the array value is as follows :? Php $ arrarray (a you, B me, he); $ arr [] others; echo $ arr [B]. br; $ arr [c]; returns a null value, but still occupies instance 7: basic operations on array values
The code is as follows:
$ Arr = array ('a' => "You", 'B' => "I", "he ");
$ Arr [] = "other ";
Echo $ arr ['B']."
";
$ Arr ['c'] = ""; // returns a null value, but still occupies the position
Echo count ($ arr )."
"; // The number of values in the array.
Unset ($ arr ['B']); // This function can deregister a string, the entire array equivalent type, and the reference type.
Print_r ($ arr); // This function prints the entire internal structure of the numeric value and reference type.
Echo"
";
Foreach ($ arr as $ key => $ value)
Echo $ key. ":". $ value ."
"; // Cyclically outputs the value of the entire array.
?>
Instance 8: convert arrays and strings
The code is as follows:
$ Arr = array ('a' => "You", 'B' => "I", "he ");
Echo $ arr = implode ('-', $ arr); // Convert an array to a string, connector-
Echo"
";
Print_r (explode ('-', $ arr, 2); // convert a string to an array. If the last parameter is not used, all '-' are split into arrays.
?>
Instance 9: array sorting
The code is as follows:
$ Arr = array ('B' => "You", 'a' => "I", "he ");
Ksort ($ arr); // pinyin (UTF-8-encoded) sorting of array key values, key values will not be lost. Note that this sorting does not return a new array, but directly transmits the original array as a reference.
Print_r ($ arr );
Echo"
";
Asort ($ arr); // The array is sorted by value in pinyin (UTF-8 encoding), and the key value is not lost. If you do not want a key value, you can use the function sort (); if there is also a function rsort () in reverse order (). Note that this sorting does not return a new array, but directly transmits the original array as a reference.
Print_r ($ arr );
Echo"
";
$ Arr = array (10000,100,100 0 );
Natsort ($ arr); // sort values by numbers, while natcasesort () is case insensitive.
Print_r ($ arr );
Echo"
";
Print_r (array_reverse ($ arr); // reverse array order
Echo"
";
?>
Example 10: Array, random number extraction, number and encoding conversion
The code is as follows:
$ Arr = array ('B' => "You", 'a' => "I", "he ");
$ Key = array_rand ($ arr, 2); // The array randomly extracts two key values and returns an index array containing two key values.
Echo $ arr [$ key [0]. $ arr [$ key [1];
Echo"
";
Echo mt_rand (60,100); // returns a random integer in this range.
Echo"
";
Echo chr (mt_rand (ord ('A'), ord ('Z'); // converts numbers and encodings.
Echo"
";
?>
The array function is over. I just want to pick out a few representative functions to help you get started. In fact, there are also some uncommonly used array functions. In addition, we can use for or foreach loops to process arrays and generate our own my _ functions. this is a stupid method.
The pipeline code is as follows :? Php $ arr = array ('a' = "you", 'B' = "I", "he"); $ arr [] = "other "; echo $ arr ['B']. "br"; $ arr ['c'] = ""; // null value, but still occupied...