CodeIgniter 中棘手的关联数组

发布于 2024-12-07 07:58:49 字数 1010 浏览 0 评论 0原文

我正在尝试为提交数据的移动设备制作条形图。每分钟,每个移动设备都会向 Web 服务器发送一个数据包,然后将其存储在 MySQL 数据库中。每个移动设备都分配有一个IP地址,每个IP地址每分钟可以多次发送数据(有时多达10次)。示例表如下所示:

date_received | bytes | IP address
----------------------------------
1314831600    | 100   | 1482747555
1314831600    | 990   | 1482747555
1314831600    | 074   | 1482747555
1314831660    | 420   | 1482747555
1314831660    | 183   | 1482747555

因此您可以看到,一个 IP 地址可以在几个小时(因此是多个分钟)内每分钟提交多次。我将如何创建一个如下所示的关联数组:

array
(
   1314831600 = array
                (
                   1482747555 => 100,
                   1482747555 => 990,
                   1482747555 => 074
                );
   1314831660 = array
                (
                   1482747555 => 420,
                   1482747555 => 183
                );
);

第一个键是 date_received 值,以及该时间发送的 IP 地址(及其相应的字节值)。我正在使用 CodeIgniter,并且考虑过在 foreach 数据库循环中填充数组,但不太确定如何最好地做到这一点。有人有什么建议吗?

注意:我需要将数据库调用保持在最低限度,因为某些表包含数十万个值。

I'm trying to make a bar chart for a mobile devices that submit data. Every minute, each mobile device sends a packet of data to the web server - where it's then stored in a MySQL database. Each mobile device is assigned an IP addresses, and each IP address can send data multiple times a minute (sometimes as many as 10). Here is what an example table would look like:

date_received | bytes | IP address
----------------------------------
1314831600    | 100   | 1482747555
1314831600    | 990   | 1482747555
1314831600    | 074   | 1482747555
1314831660    | 420   | 1482747555
1314831660    | 183   | 1482747555

So you can see that one IP address can submit multiple times a minute over a span of hours (therefore multiple minutes). How would I create an associative array that looked like this:

array
(
   1314831600 = array
                (
                   1482747555 => 100,
                   1482747555 => 990,
                   1482747555 => 074
                );
   1314831660 = array
                (
                   1482747555 => 420,
                   1482747555 => 183
                );
);

The first key would be the date_received value, and the the IP addresses which are sent for that time (with their corresponding bytes values). I'm using CodeIgniter and I thought about populating arrays in my foreach database loop, but wasn't quite sure how best to do this. Does anybody have any advice?

N.B: I need to keep database calls to a minimum because some tables contain hundreds of thousands of values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2024-12-14 07:58:49

您不能共享这样的数组键(IP 地址),因为它们将被覆盖。您可以执行以下操作:

$packets = array();
while ($row = mysql_fetch_assoc($res)) {
    $packets[$row['date_received']][] = 
        array('ip_address'=>$row['ip_address'], 
        'bytes'=>$row['bytes']
    );
}

然后您可以使用以下方式循环数据:

foreach ($packets as $date => $info) {
    echo "date = $date, ip = $info[ip_address], bytes = $info[bytes]";
}

You cannot share array keys like that (ip address) as they will be overwritten. You can do something like:

$packets = array();
while ($row = mysql_fetch_assoc($res)) {
    $packets[$row['date_received']][] = 
        array('ip_address'=>$row['ip_address'], 
        'bytes'=>$row['bytes']
    );
}

Then you can loop through the data with:

foreach ($packets as $date => $info) {
    echo "date = $date, ip = $info[ip_address], bytes = $info[bytes]";
}
甜`诱少女 2024-12-14 07:58:49

如果你像这样重写数组的一部分,问题就会变得容易得多:

        array
            (
               1482747555 => array(100,990,074)
            );

If you rewrite part of your array like this, the problem becomes much easier for you:

        array
            (
               1482747555 => array(100,990,074)
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文