CodeIgniter 中棘手的关联数组
我正在尝试为提交数据的移动设备制作条形图。每分钟,每个移动设备都会向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能共享这样的数组键(IP 地址),因为它们将被覆盖。您可以执行以下操作:
然后您可以使用以下方式循环数据:
You cannot share array keys like that (ip address) as they will be overwritten. You can do something like:
Then you can loop through the data with:
如果你像这样重写数组的一部分,问题就会变得容易得多:
If you rewrite part of your array like this, the problem becomes much easier for you: