1. Array () function for creating arrays
In PHP, there are three types of arrays:
- Indexed array-an array with a numeric index
- Associative array-An array with the specified key
- Multidimensional arrays-An array that contains one or more arrays
Since PHP 5.4, you can use the short array syntax and replace array () with [].
For example, use $cars =["Volvo", "BMW"); Instead of $cars =array ("Volvo", "BMW");
2. The Array_change_key_case () function converts all keys in an array to uppercase or lowercase letters
Array_change_key_case (array,case);
The numeric index of the array does not change. If an optional parameter (that is, the second parameter) is not supplied, the default is converted to lowercase letters (
- Case_lower-Default value. Converts the keys of an array to lowercase letters.
- Case_upper-Converts the key of the array to uppercase.
)
If two or more keys are the same when you run the function, the last element overrides the other elements
3. The Array_chunk () function splits an array into a new array block
Array_chunk (array,size,preserve_key);
The number of cells in each of these arrays is determined by the size parameter. The number of cells in the last array may be few.
An optional parameter, Preserve_key , is a Boolean value that specifies whether the elements of the new array have the same key as the original array (for associative arrays) or a new numeric key starting at 0 (for an indexed array). The default is to assign a new key.
- True-preserves the key names in the original array.
- False-Default. Each result array uses a new zero-based index of the array.
4. Array_column () returns the value of a single column in the input array
Array_column (array,column_key,index_key);
To remove the Last_Name column from the recordset:
Represents the array of possible recordsets returned by the database $ A = array (' id ' = ' = ' 5698, ' first_name ' = ' Bill ', ' last_name ' = ') Gates ', ), array ( ' id ' = = 4767, ' first_name ' = ' Steve ', ' last_name ' = ' Jobs ', ) Array ( ' id ' = = 3809, ' first_name ' = ' Mark ', ' last_name ' = ' Zuckerberg ', )); $last _ names = Array_column ($a, ' last_name ');p rint_r ($last _names);
Output:
Array ( [0] = Gates [1] = Jobs [2] = Zuckerberg)
Remove the Last_Name column from the recordset and use the corresponding "id" column as the key value:
Represents the array of possible recordsets returned by the database $ A = array (' id ' = ' = ' 5698, ' first_name ' = ' Bill ', ' last_name ' = ') Gates ', ), array ( ' id ' = = 4767, ' first_name ' = ' Steve ', ' last_name ' = ' Jobs ' ) Array ( ' id ' = = 3809, ' first_name ' = ' Mark ', ' last_name ' = ' Zuckerberg ', )); $ Last_names = Array_column ($a, ' last_name ', ' id ');p rint_r ($last _names);
Output:
Array ( [5698] = Gates [4767] = Jobs [3809] = Zuckerberg)
PHP array functions (1)