Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按范围值对数组进行分组_Php_Arrays_Laravel 5 - Fatal编程技术网

Php 按范围值对数组进行分组

Php 按范围值对数组进行分组,php,arrays,laravel-5,Php,Arrays,Laravel 5,我有这个数组[1,1,2,2,3,4,4,5,6,6,6,7],我可以根据范围值对数组进行分组,从而得到最终结果: '1-3'=[1,1,2,2,2,3];//计数是6 '4-5' = [4,4,5]; // 计数是3 '6-7' = [6,6,6,7]; // 计数为4使用您的范围创建一个新数组,然后遍历值和其中的范围。如果当前值在范围内,请将记录添加到当前范围: <?php $numbers = [1,1,2,2,2,3,4,4,5,6,6,6,7]; $counts = []; $c

我有这个数组[1,1,2,2,3,4,4,5,6,6,6,7],我可以根据范围值对数组进行分组,从而得到最终结果:

'1-3'=[1,1,2,2,2,3];//计数是6
'4-5' = [4,4,5]; // 计数是3

'6-7' = [6,6,6,7]; // 计数为4

使用您的范围创建一个新数组,然后遍历值和其中的范围。如果当前值在范围内,请将记录添加到当前范围:

<?php
$numbers = [1,1,2,2,2,3,4,4,5,6,6,6,7];
$counts = [];
$counts[] = ["values"=> [1, 3], "records" => []]; // first value in "values" is min, second is max
$counts[] = ["values"=> [4, 5], "records" => []];
$counts[] = ["values"=> [6, 7], "records" => []];
foreach ($numbers as $number) {
    foreach ($counts as $key => &$value) {
        if ($number >= $value["values"][0] && $number <= $value["values"][1]) { // if between the range, add it to the records
            $value["records"][] = $number;
        }
    }
}
echo "<pre>";
foreach ($counts as $count) {
    echo $count["values"][0]." - ".$count["values"][1]." = ".count($count["records"])." elements<br>";
}

我相信你需要的是:

function count_range($array, $min, $max) {
    return sizeof(array_intersect($array, range($min, $max)));
}

$array = [1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7];

echo count_range($array, 1, 3);  // 6 from [1, 1, 2, 2, 2, 3]
echo count_range($array, 4, 4);  // 2 from [4, 4]
echo count_range($array, 2, 7);  // 11 from [2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7]
函数数组\u获取\u范围($array,$min,$max){
返回数组\过滤器($array,function($element)use($min,$max){

return$element>=$min&&$element我认为
array\u intersect()
range()
with
sizeof()/count()
可以更清晰地完成此任务。它消除了双重条件

代码:()

  • range()
    创建一个或多个连续整数的白名单数组
  • array\u intersect()
    一次比较输入数组和白名单数组
  • sizeof()
    只是
    count()
    的别名,用于确定保留了多少元素

是的,你可以。范围是如何定义的?它们可以改变吗?你希望结果是什么?你想计数还是分组?问题有点模糊。嘿@salathe,对不起,我的英语不好,我在DB中有“User”表,列为“birth_date”。是的,我想将年龄为{10-20岁,21-30岁,等等]的用户分组,然后计算特定年龄段的总用户数。我想创建一个类似于此年龄段的HTML表:总用户数10到20,总用户数21到30,总用户数58
function count_range($array, $min, $max) {
    return sizeof(array_intersect($array, range($min, $max)));
}

$array = [1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7];

echo count_range($array, 1, 3);  // 6 from [1, 1, 2, 2, 2, 3]
echo count_range($array, 4, 4);  // 2 from [4, 4]
echo count_range($array, 2, 7);  // 11 from [2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7]