Home  >  Article  >  Backend Development  >  How to find the sum of some elements in an array in php

How to find the sum of some elements in an array in php

PHPz
PHPzOriginal
2023-04-20 10:14:54588browse

Recently, I encountered a problem when developing a website. I need to get some data from an array for display on the website. This array contains many elements, but only some of them are what I need. After constant attempts and research, I finally found a solution, which is the array_slice() function in PHP. In this article, I will introduce to you in detail the usage and implementation principle of this function.

First of all, we need to understand the basic syntax of the array_slice() function. Its syntax is as follows:

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] )

Among them, $array is the array we want to process, $offset represents the starting position of the data we want to obtain, $length represents the length of the data we want to obtain, if not specified, it defaults Get all the data from $offset to the end of the array, $preserve_keys indicates whether to retain the key names of the elements in the original array. The return value of this function is a new array containing the specified portion of data.

Next, let’s take a look at how to use this function. Suppose we have an array $students that contains a lot of student information. Each element contains the student's name, student number, gender, age and other information. We need to get the names and student numbers of the first 10 students, then we You can write the code like this:

$students = array(/*这里是很多个学生信息*/);
$part_students = array_slice($students, 0, 10); //获取前10个学生的数据
$name_id = array_map(function($s) { //通过array_map()函数,将每个学生的姓名和学号取出来
  return array(
    'name' => $s['name'],
    'id' => $s['id']
  );
}, $part_students);

//将数据在网站上进行展示
foreach($name_id as $info) {
  echo "姓名:".$info['name']." 学号:".$info['id']."<br/>";
}

With the above code, we can get the names and student numbers of the first 10 students from the $students array and display them on the website. The above code also uses the array_map() function. Friends who are not familiar with it can search and learn it by themselves.

In addition to getting a part of the array, the array_slice() function can also use negative numbers to represent the countdown position from the end of the array. For example, if we want to get the data of the last 10 students in the array, we can write like this:

$part_students = array_slice($students, -10);

In this way, the $part_students array contains the data of the last 10 students in the $students array.

Finally, let’s explore the implementation principle of the array_slice() function. In fact, the implementation of this function is very simple, mainly using the array_chunk() and array_merge() functions in PHP. Taking the first 10 elements of an array as an example, the internal implementation of the function is roughly as follows:

function array_slice($array, $offset, $length = NULL, $preserve_keys = FALSE) {
  $len = count($array);
  if($offset < 0) { //如果$offset是负数,就转化成从数组末尾倒数的位置
    $offset = $len + $offset;
  }
  if($length === NULL) { //如果$length没指定,就获取从$offset到数组末尾的所有数据
    $length = $len - $offset;
  }
  $chunk = array_chunk($array, $length, $preserve_keys); //将数组按指定长度分块
  $part = $chunk[0]; //获取第一块的数据
  if(!$preserve_keys) { //如果不保留键名,重新索引数组
    $part = array_values($part);
  }
  return $part;
}

Among them, the array_chunk() function divides the array into several blocks according to the specified length, and each block is a sub-array, and The array_merge() function merges multiple subarrays into an overall array. By combining these two functions, the array_slice() function can achieve the function of obtaining part of the data from the array.

In actual development, we often need to obtain part of the data from a large amount of data for display. At this time, the array_slice() function is very convenient. It can quickly cut the data we want to process into the parts we need. At the same time, because it is a built-in function of PHP, its execution efficiency is also very high.

The above is the detailed content of How to find the sum of some elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn