数组是空的,我不知道为什么


array is empty and i dont know why

我正在尝试制作一个包含100个索引的数组的直方图,其中允许重复数字。我需要另一个数组$estastisticas(),它告诉数组$histogram()中存在n的次数。例如

我有这个:$直方图(1,1,1,2,1,2,3,2,2)我想要这个:$estastisticas(0,4,4,1)

换句话说,

数组$histogram,数字0(或$estastisticas(0))出现0次,数字1(或$destesticas(1。

这是我的代码,但当我制作var_dump($estatisticcas)时;在代码的末尾(或代码的任何其他部分),我得到这个错误消息数组(大小=0)空

为什么数组$estastisticas是空的?我做错了什么?感谢您抽出时间

<?php
//vars
$histogram = array();
$estatisticas = array();
$num = 0;
$count = 0;
//connection do database
include('connect_db.php'); etc... everything fine here
//insert into array $histogram() values from database... everything fine here
while($row = mysqli_fetch_array($fetch)){                           
    array_push($histogram, 
    $row['primeiro_numero'], //table names... everything fine here
    $row['segundo_numero'],  //table names... everything fine here
    $row['terceiro_numero'], //table names... everything fine here
    $row['quarto_numero'],   //table names... everything fine here
    $row['quinto_numero']);  //table names... everything fine here
}
//conversion from array $histogram() STRING into INT... everything fine here
//final result is $histogram() is all in INT... everything fine here
for( $i = 0; $i < count($histogram); $i++){    
    $histogram[$i] = (int) $histogram[$i];
}
for ($i = 0; $i <= 50; $i++) {
    $num = 0;
    $count = 0;
    do {
    if ($histogram[$i] = $count) {
        $num = $histogram[$i];
        $estatisticas[$num]++;
        } 
    else {
        $count = $count+1;
        }   
    } while ($histogram[$i] != $num);//end while
}//end for
//PROBLEM HERE
var_dump($estatisticas); //error message "array (size=0) empty"
?>


你的$estatisticas没有正确安装,我对你的代码做了一些修改,希望这对有帮助

<?php
//vars
$histogram = array();
$estatisticas = array();
$num = 0;
$count = 0;
//connection do database
include('connect_db.php'); etc... everything fine here
//insert into array $histogram() values from database... everything fine here
while($row = mysqli_fetch_array($fetch)){                           
    array_push($histogram, 
    $row['primeiro_numero'], //table names... everything fine here
    $row['segundo_numero'],  //table names... everything fine here
    $row['terceiro_numero'], //table names... everything fine here
    $row['quarto_numero'],   //table names... everything fine here
    $row['quinto_numero']);  //table names... everything fine here
}
//conversion from array $histogram() STRING into INT... everything fine here
//final result is $histogram() is all in INT... everything fine here
for( $i = 0; $i < count($histogram); $i++){    
    $histogram[$i] = (int) $histogram[$i];
}
for ($i = 0; $i <= 50; $i++) {
    $num = 0;
    $count = 0;
    do {
    if ($histogram[$i] = $count) {
        $num = $histogram[$i];
        (is_null($estatisticas[$num]) || empty($estatisticas[$num])) ? $estatisticas[$num] = 1 : $estatisticas[$num]++;
        } 
    else {
        $count = $count+1;
        }   
    } while ($histogram[$i] != $num);//end while
}//end for
//PROBLEM HERE
var_dump($estatisticas); //error message "array (size=0) empty"
?>

我会这样做:

$estatisticas = array_count_values($histogram);
for($i=0;$i<100;$i++) {
    if(!isset($estatisticas[$i])) {
        $estatisticas[$i] = 0;
    }
}