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

时间:2014-11-11 18:52:14

标签: php html arrays runtime-error histogram

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

我有这个:$ histogram(1,1,1,2,1,2,3,2,2) 我想要这个:$ estatisticas(0,4,4,1)

换句话说

数组$ histogram,数字0(或$ estatisticas(0))出现0次,数字1(或$ estatisticas(1))出现4次,数字2(或$ estatisticas(2))出现4次数,3次(或$ estatisticas(3))出现1次。

这是我的代码,但是当我创建一个var_dump($ estatisticas);在代码的末尾(或代码的任何其他部分),我得到此错误消息数组(size = 0)为空

为什么数组$ 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];
        $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"
?>

2 个答案:

答案 0 :(得分:0)


您的$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"
?>

答案 1 :(得分:0)

我会像这样做这个任务:

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