Home > Article > Backend Development > Data analysis function of PHP function
PHP is a high-performance, open source scripting language widely used in web development. With the development of data analysis technology, more and more data need to be processed and analyzed. PHP provides some data analysis functions that can be used to process various types of data. This article will introduce how to use PHP data analysis functions and examples.
1. Statistical function
Example:
$numbers = array(1, 2, 3, 4, 5); echo count($numbers); // 输出 5
Example:
$numbers = array(1, 2, 3, 4, 5); echo array_sum($numbers); // 输出 15
Example:
$numbers = array(1, 2, 3, 4, 5); echo array_sum($numbers) / count($numbers); // 输出 3
2. Sorting function
Example:
$numbers = array(5, 4, 3, 2, 1); sort($numbers); print_r($numbers); // 输出Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Example:
$numbers = array(1, 2, 3, 4, 5); rsort($numbers); print_r($numbers); // 输出Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )
3. Filter function
Example 1:
$numbers = array(1, 2, 3, 4, 5); $new_numbers = array_filter($numbers, function($value) { return $value % 2 == 0; // 只保留偶数 }); print_r($new_numbers); // 输出Array ( [1] => 2 [3] => 4 )
Example 2:
$employees = array( array('name' => '张三', 'age' => 25, 'gender' => '男'), array('name' => '李四', 'age' => 30, 'gender' => '女'), array('name' => '王五', 'age' => 35, 'gender' => '男') ); $new_employees = array_filter($employees, function($employee) { return $employee['gender'] == '男'; // 只保留男员工 }); print_r($new_employees); // 输出Array ( [0] => Array ( [name] => 张三 [age] => 25 [gender] => 男 ) [2] => Array ( [name] => 王五 [age] => 35 [gender] => 男 ) )
Example:
$numbers = array(1, 2, 1, 3, 2, 4, 5); $new_numbers = array_unique($numbers); print_r($new_numbers); // 输出Array ( [0] => 1 [1] => 2 [3] => 3 [5] => 4 [6] => 5 )
4. String function
Example:
$string = 'Hello world'; echo strlen($string); // 输出 11
Example:
$string = 'Hello world'; echo strpos($string, 'o'); // 输出 4
Example:
$string = 'Hello world'; echo substr($string, 6, 5); // 输出 world
The above are some common examples of PHP data analysis functions. Through them, we can implement functions such as array statistics, sorting, filtering, and string operations. In actual projects, these functions can be combined for comprehensive analysis and processing according to different needs to improve the utilization value of data.
The above is the detailed content of Data analysis function of PHP function. For more information, please follow other related articles on the PHP Chinese website!