How to find the multiple of an array in php

PHPz
Release: 2023-04-12 11:26:12
Original
756 people have browsed it

In PHP, to require multiples in an array, you can use the method of looping through the array. The following code can be used for specific implementation:

//定义一个数组
$numbers = array(1,2,3,4,5,6,7,8,9,10);

//定义一个变量存储倍数
$multiple = 3;

//定义一个新的数组
$result = array();

//循环遍历原数组
foreach($numbers as $num){
    //如果当前元素是倍数
    if($num % $multiple == 0){
        //将其加入新的数组
        $result[] = $num;
    }
}
//输出新数组
print_r($result);
Copy after login

In the above code, we first define an original array $numbers, which contains the numbers 1~10. Then, we defined a variable $multiple to represent the multiple of the required number in the array, here we chose 3. Next, we define a new array $result to store the results.

Loop through the original array $numbers through foreach, and perform a modulo operation on each element with the multiple $multiple. When the remainder is 0, it means that the current element is a multiple, and it is added to the new array $result. Finally, we use print_r to output the new array $result. You can see that the result is:

Array ( [0] => 3 [1] => 6 [2] => 9 )
Copy after login

The above code can effectively solve the multiples in the array. . Of course, we can also encapsulate it into a function to make it easier to call. The following is the encapsulated code:

/**
 * 求数组中的倍数
 * @param array $numbers 原数组
 * @param int $multiple 倍数
 * @return array 结果数组
 */
function getMultiple($numbers, $multiple){
    //定义新数组
    $result = array();

    //循环遍历原数组
    foreach($numbers as $num){
        //如果当前元素是倍数
        if($num % $multiple == 0){
            //将其加入新数组
            $result[] = $num;
        }
    }
    //返回结果数组
    return $result;
}

//测试代码
$numbers = array(1,2,3,4,5,6,7,8,9,10);
$result = getMultiple($numbers, 3);
print_r($result);
Copy after login

In the encapsulated code, we pass the original array $numbers and the multiple $multiple as parameters into the functiongetMultiple in. The implementation in the function is the same as the previous code, except that comments and descriptions in the function header are added. Finally, we output the result array $result, and you can see that the results are the same as before.

In practical applications, we can also extend this function to consider more situations. For example, for different solution methods, you can set the parameter $type in the function to indicate whether to find multiples or divisors, etc. In short, with the help of PHP's flexible syntax, we can easily solve the numbers in the array.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template