PHP数组键索引为什么显示随机

时间:2019-03-08 09:54:48

标签: php

在php中为什么数组键索引显示随机(天值)

function getCustomHour($totals) {
    $startMonth = date("Y/m/", $this->startDate);
    $endMonth = date("Y/m/", $this->endDate);
    $data = array();
    $maxCount = array();
    if (empty($totals)) {
        return $data;
    }
    foreach ($totals as $testData) {
        $today = $testData['day'];
        $count = $testData['count'];
        if (empty($maxCount[$today]) || $count > $maxCount[$today]) {
            $maxCount[$today] = $count;
            if ($today < 10) {
                $data[$today]["Day"] = $endMonth . "0" . $today;
            } else {
                $data[$today]["Day"] = $startMonth . $today;
            }
            $data[$today]['Hour'] = $testData['hour'];
            $data[$today]['Count'] = $testData['count'];
        }
    }

    echo "<pre>";
    print_r($data);
    echo "</pre>";
    return $data;
}

Array
(
    [3] => Array
        (
            [Day] => 2019/03/03
            [Hour] => 23
            [Count] => 9
        )

    [4] => Array
        (
            [Day] => 2019/03/04
            [Hour] => 23
            [Count] => 10
        )

    [5] => Array
        (
            [Day] => 2019/03/05
            [Hour] => 23
            [Count] => 6
        )

    [7] => Array
        (
            [Day] => 2019/03/07
            [Hour] => 23
            [Count] => 5
        )

)

为什么显示3,4,5,7而不是0,1,2,3

我不知道出什么问题

我没有使用 rand 函数,但是我无法使用

这是什么问题?如何显示此0,1,2

我不知道出什么问题

该如何解决?

1 个答案:

答案 0 :(得分:1)

因为,您正在向<div> ***remove*** <p>A car is a wheeled, self-powered motor vehicle used for transportation.</p> ***remove*** <p>A car is a wheeled, self-powered motor vehicle used for transportation.</p> ***remove*** <p>A car is a wheeled, self-powered motor vehicle used for transportation.</p> </div>提供密钥$today到密钥$data

您可以做的是:在0上使用array_values()

在返回$data之前,请重置您的密钥。

这样您的密钥将从$data0

1,2,3,...
  

array_values

     

(PHP 4,PHP 5,PHP 7)array_values —返回数组的所有值

     

说明      

array_values(array $ array):array array_values()返回所有   数组中的值并以数字方式对数组进行索引。