array
An array in php is an ordered mapping, a type that associates values to keys, and an array can accept any number of key-value pairs separated by commas.
Introduction to arrays
/* * Arrays in php can be divided into: *Index Array: Subscript is a number *Associated Array: Subscript is string *Note: Arrays in php don't really distinguish between indexed and associated arrays, they all find corresponding values based on key names * * php can contain both integer and string key names * key: can be an integer or string * value: can be any type of value * * The key in php can only be integer or string type, which will be automatically converted if other types are used: * 1. Strings containing valid integer values are automatically converted to integers (e.g.'8'is converted to 8, but'08' is not converted) * 2. Floating-point numbers are converted to integers and decimal parts are discarded * 3. Booleans are converted to integers (true to 1, false to 0) * 4. NULL is converted to an empty string,'' * 5. Arrays and objects cannot be used as key names * *If multiple cells in the array definition have the same key name, only the last one will be used and the previous values will be overwritten *If no key name is specified and not all keynames are negative, the newly added element's key name is the maximum of the existing key name+1 *If the key names are negative, the newly added element key names start at 0 * */
Array creation
$arr1 = array(); //Create an empty array $arr2 = array(1, 2, 3); //Create an index array $arr3 = array( //Create an associated array 'a' => 'a_value', 'b' => 'b_value', 'c' => 'c_value' ); $arr4 = []; //[] is the same as array(), you can also create empty arrays, index arrays, associated arrays $arr4[] = 'a'; //Extend the array with [] to add new elements to the array
Automatic conversion of key name types
$arr = array( 1 => 'a', //1 key is named integer type and will not be converted automatically 'b' => 'b', //'b'key is named string type and will not be converted automatically '8' => 'c', //8 Strings containing valid integer values are automatically converted to integers 3.5 => 'd', //The 3-key, named floating-point type, will round off decimals and convert to integer type true => 'e', //1 key named Boolean type, true to 1, false to 0 null => 'f', //The''key is named NULL type and will be converted to'' 'g' //9 No key name specified, default to the value of maximum existing key name+1 ); $arr[] = 'h'; //10 Add a new key pair without specifying a key name. Default is the value with the largest existing key name+1 print_r($arr); //printf_r() is a special function for array output
Quickly create with range() and compact()
//range() to quickly create indexed arrays with consecutive Subscripts // /* * range()function * array range(mixed $start, mixed $limit[, number $step=1]) * Description: Creates an indexed array containing a specified range cell with consecutive Subscripts * start: First value of the sequence * limit: Value of sequence ending at limit * step: If a step value is given, it will be used as a step value between cells. If not specified, it defaults to 1 * * Supplement: * string chr(int $value): Converting Numbers to Characters as ASCII * int ord(string $value): ASCII for Output Characters */ $arr = range(1, 5); print_r($arr); //Array([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) $arr = range('a', 'b', 'c'); print_r($arr); //Array([0] => a [1] => b [2] => c) echo chr(90), "\n"; //Z echo ord('z'), "\n"; //122 //compact() to quickly create associative arrays /* * compact()function * array compact(mixed $varname[, mixed $...]) * Description: Create an array including variable names and their values * * Supplement: * list($var1, $var2...): Assign values in an array to some variables */ $username = 'zhao'; $age = '22'; $email = 'wangzhao_hb@126.com'; $arr = compact('username', 'age', 'email'); //Quickly generate an associated array by placing an existing variable name in a function //Array([username] => zhao [age] => 22 [email] => wangzhao_hb@126.com) print_r($arr); list($a, $b, $c) = array(1, 2, 3); echo "a The value of{$a}, b The value of{$b}, c The value of{$c}", "\n"; //A has a value of 1, b has a value of 2, c has a value of 3
Defining a constant array by const and define()
const ARR1 = [1, 2, 3]; //const defines a constant array print_r(ARR1); //Array([0] => 1 [1] => 2 [2] => 3) define('ARR2', [1, 2, 3]); //define() defines a constant array print_r(ARR2); //Array([0] => 1 [1] => 2 [2] => 3) //Use scenarios: custom file upload error array information define('CUSTOM_UPLOAD_ERRORS', [ 'ext_error' => 'File extension does not conform to specification', 'maxsize' => 'Upload file size does not conform to specifications' ]); //Error can be invoked when file upload encounters an extension that does not conform to the specification echo CUSTOM_UPLOAD_ERRORS['ext_error'], "\n"; //File extension does not conform to specification
Use of arrays
/* * => Find corresponding key value by key name * Add, delete, change, check */ //Find: Find the corresponding key value by the key name $arr1 = ['a', 'b', 'c', 'd']; $arr2 = [ 'username' => 'Wang Zhao', 'sex' => 'male' ]; echo 'Value with subscript 2:', $arr1[2], "\n"; //Subscript 2 values:c echo 'User name:',$arr2['username'], "\n"; //User name: Wang Zhao $arr3 = [ //Binary array lookup ['id'=>'1001', 'name'=>'Zhang San'], ['id'=>'1002', 'name'=>'Li Si'] ]; echo 'Name with subscript 0:', $arr3[0]['name'], "\n"; //Name with subscript 0: Zhang San //Add to $arr = ['a', 'b', 'c']; $arr[] = 'd'; //Add element, do not specify key name, key name is number $arr['username'] = 'Wang Zhao'; //Add element, specify key name, key name is string print_r($arr); //Array ([0] => a [1] => B [2] => C [3] => D [username] => Wang Zhao) //modify $arr = ['a', 'b', 'c', 'username'=>'Wang Zhao']; $arr['username'] = 'Zhang San'; echo $arr['username'], "\n"; //Zhang San //delete unset($arr['username']); //Delete key-value pairs with key name'username' print_r($arr); //Array([0] => a [1] => b [2] => c) unset($arr[1]); print_r($arr); //Array([0] => a [2] => c) unset($arr); //Release Array //print_r($arr); //Output error Notice: Undefined variable
Convert other types to arrays
/* * If you convert a value to an array, you get an array with only one element, and its subscript is 0, which is the value of this scalar. * Objects can be converted to arrays, but be careful not to do so because it can lead to unexpected effects. * Note: null conversion to array is an empty array [] * * Temporary conversion * (array)$var * * convert permanently * settype($var, 'array') */ $str = 'abc'; $arr = (array)$str; print_r($arr); //Array([0] => abc) settype($str, 'array'); print_r($str); //Array([0] => abc)
Array Operations
/* * Common array operators: * +: Merge arrays, showing only the key-value pairs of the left array if the key names are the same * ==: Compares whether the key names and corresponding key values of arrays are the same, returns true if they are the same, or false if they are not * ===: Compare the key name with the corresponding key value and key value type in the same order * !=: Compare arrays'key names to their corresponding key values * !==: Compare arrays'key names with corresponding key values and key types, or in different order * <>: Same as!= * * */ $arr1 = [1, 2, 3]; $arr2 = ['a', 'b', 'c']; $arr3 = ['username'=>'Zhang San', 'age'=>12]; $arr4 = [10=>10, 11=>11]; $arrSum1 = $arr1 + $arr2; //If the key names are the same, only the key-value pairs of the left array will be displayed $arrSum2 = $arr1 + $arr3; //+independent of whether the array is an indexed array or an associated array $arrSum3 = $arr1 + $arr3 + $arr4; //If the key name of the following array and the key name of the preceding array are duplicated, they will not be overwritten and the corresponding key value of the preceding array will be displayed print_r($arrSum1); //Array([0] => 1 [1] => 2 [2] => 3) print_r($arrSum2); //Array ([0] => 1 [1] => 2 [2] => 3 [username] => Zhang San [age] => 12) print_r($arrSum3); //Array ([0] => 1 [1] => 2 [2] => 3 [username] => Zhang San [age] => 12 [10] => 10 [11] => 11) $arr5 = ['1'=>1, 'b'=>2, 'c'=>3]; $arr6 = ['b'=>2, '1'=>1, 'c'=>3]; $arr7 = [1=>1, 'b'=>2, 'c'=>3]; var_dump($arr5 == $arr6); //bool(true) var_dump($arr5 === $arr6); //bool(false) var_dump($arr5 === $arr7); //bool(true) var_dump($arr5 != $arr6); //bool(false) var_dump($arr5 !== $arr6); //bool(true)