Php practical functions: php functions. Php practical functions: for the first time, write some practical functions in your work. Array Function: 1array_column -- return the specified column in the array. usage 1: php return function, php function
For the first time, write some useful functions in your work.
Array functions:
1 array_column -- return the specified column in the array
Usage 1: return a column specified in the array
Application scenario: retrieve the id of the class and query other information of the class from other tables.
Benefit: reduce the number of times the database is queried (all information is retrieved at one time by id)
Column:
$ Records = array (
Array (
'Id' => 2135,
'First _ name' => 'John ',
'Last _ name' => 'Doe ',
),
Array (
'Id' => 3245,
'First _ name' => 'Sally ',
'Last _ name' => 'Smith ',
)
);
$ First_names = array_column ($ records, 'id ');
Print_r ($ first_names );
// Result
Array ('20140901', '20140901 ');
?>
Usage 2: use the id in a two-dimensional array as the key of the array
Application scenario: Retrieve data using array ['id ']
Benefit: use array_column instead of foreach to use the id in the two-dimensional array as the key of the array. the code is concise and calls system functions to improve efficiency.
Column:
$ Records = array (
Array (
'Id' => 2135,
'First _ name' => 'John ',
'Last _ name' => 'Doe ',
),
Array (
'Id' => 3245,
'First _ name' => 'Sally ',
'Last _ name' => 'Smith ',
)
);
// Use foreach
$ Records_key = array ();
Foreach ($ records as $ k => $ v ){
$ Records_key [$ v ['id'] = $ v;
}
Print_r ($ first_names );
// Call array_column
$ Records_key = array_column ($ records, NULL, 'id ');
Print_r ($ first_names );
?>
// Result
Array (
[2135] => Array (
[Id] = & gt; 2135
[First_name] => John
[Last_name] => Doe
)
[3245] => Array (
[Id] = & gt; 3245
[First_name] => Sally
[Last_name] => Smith
)
)
2 array_multisort -- sorts multiple arrays or multi-dimensional arrays
Usage 1: multi-dimensional array sorting
Application scenario: sort data by Price in descending order and by sales volume in ascending order
Benefits:
Column:
$ Data [] = array ('price' => 67, 'Sale _ num' => 2 );
$ Data [] = array ('price' => 86, 'Sale _ num' => 1 );
$ Data [] = array ('price' => 85, 'Sale _ num' => 6 );
$ Data [] = array ('price' => 98, 'Sale _ num' => 2 );
$ Data [] = array ('price' => 86, 'Sale _ num' => 6 );
$ Data [] = array ('price' => 67, 'Sale _ num' => 7 );
// Retrieve the column list
$ Price = array_column ($ data, 'price ');
$ Sale_num = array_column ($ data, 'Sale _ num ');
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ price, SORT_DESC, $ sale_num, SORT_ASC, $ data );
?>
// Result
price | sale_num -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7
Not complete to be continued!
Http://www.bkjia.com/PHPjc/1117668.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117668.htmlTechArticlephp practical functions, php functions for the first time casually, write some of your work more practical functions. Array Function: 1 array_column -- return the specified column in the array. usage 1: return...