简体   繁体   English

在临时数组中添加计数值

[英]Adding count values in a temporary array

I am counting the difference between two days in inside a foreach loop. 我在foreach循环中计算两天之间的差异。

foreach ($result as $key => $value) {
    # code...

    //$temp[$value->user_id]=$value->user_id;
    $count_dates=daysBetween($value->user_source_history_added,$current_date);
    $tmp_array[$count_dates][] = $count_dates;
}

On debugging tmp_array, I get something like this. 在调试tmp_array时,我得到了类似的信息。

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
        )

    [1] => Array
        (
            [0] => 1
        )

    [3] => Array
        (
            [0] => 3
            [1] => 3
            [2] => 3
            [3] => 3
        )
)

Now I want to count the number of 0's, 1's, 2's, 3's, etc. So for now there are 7 0's and 1 1's and 4 3's. 现在我要计算0、1、2、3等的数量。因此,现在有7 0、1 1和4 3。 How do I get the count of all these numbers and how do I limit it that I get only the count till Array 20?? 我如何获得所有这些数字的计数,以及如何限制直到阵列20才获得计数?

I tried this: 我尝试了这个:

foreach($tmp_array as $tmp_val)
{
    count($tmp_val)
}

But I get the count of main array that is 3 但是我得到的主数组数是3

If you want the count of everything added together: 如果您想将所有加在一起的计数:

$count = 0;
foreach($tmp_array as $tmp_subarray)
{
    foreach($tmp_subarray as $tmp_val) {
        if($count < 20) {
            $count++;
        }
    }
}

If you want the amount of 1s, 2s, 3s, etc: 如果您想要1s,2s,3s等数量:

$count = array();
foreach($tmp_array as $tmp_subarray)
{
    foreach($tmp_subarray as $tmp_val) {
        if($count[$temp_val]) {
            $count[$temp_val]++;
        } else {
            $count[$temp_val] = 1;
        }
    }
}

This will count all the values of an array no matter how many dimensions assuming the values are arrays or numbers. 假设值是数组或数字,无论有多少维,这都会计算数组的所有值。 This will run until the main array has reached $key == 20. 这将一直运行到主数组达到$ key == 20为止。

$total = 0;

function addToTotal($number) {
    glob $total;
    $total = $total + $number;
}

function countArrayValues($array) {
    foreach($array as $key => $value) {
        if(is_array ($value) {
            countArrayValues($value);
        } else {
            addToTotal($value);
        }
    }
}

$mainArray; // <-- your array;

foreach($mainArray as $key => $value) {
    if($key <= 20) {
        countArrayValues($value);
    } else {
        break;
    }
}

echo $total;

Try this : 尝试这个 :

$array_number_of_numbert = array();
foreach($tmp_array as $tmp_val_num => $tmp_val)
{
    $array_number_of_numbert[$tmp_val_num]= $tmp_val;
}
for($i = 0; $i <=20; $i++)
{
    if(isset($array_number_of_numbert[$i])) echo count($array_number_of_numbert[$i])." ". $i."'s\n";
}

Update if($count_array<=20){} 更新if($count_array<=20){}

This is a general solution, it will take in consideration any number in you have in your $temp array and it will store this number as his record in this array 这是一个通用的解决方案,它将考虑$ temp数组中您所拥有的任何数字,并将此数字作为其记录存储在此数组中

$found_numbers=array();
$results=array();
$count_array=0;
    foreach($tmp_array as $first_array)
{   $count_array++;
    foreach($first_array as $second_array)
    { 
      if($count_array<=20){
        if (in_array($second_array, $found_numbers)) {
          $results[$second_array][0]++;
        }
        else{
         array_push($found_numbers,$second_array);
         $results[$second_array] = array();
         array_push($results[$second_array],1);
         }
      } 
    }
}

//to get how many you have number n in your array you have only to type print($results[n][0]); //要获得多少个数字n,您只需键入print($ results [n] [0]);

print($results[0][0]); //will give you 7
print($results[n][0]);  //will give the record of the number n in your array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM