[英]Retrieving values from a PHP Multi-dimensional Array
I have a multi-dimensional array that I want to print statistics from for a website admin. 我有一个多维数组,我想从网站管理员打印统计数据。 My problem is that once I get the data into an array in the format I want it, I can't figure out how to retrieve it.
我的问题是,一旦我以我想要的格式将数据导入数组,我就无法弄清楚如何检索它。 I have to know the key values in order to get it eg
$uniqueBrowser["Firefox"]["14.0.1"]['times'];
我必须知道关键值才能得到它,例如
$uniqueBrowser["Firefox"]["14.0.1"]['times'];
I want to be able to say for example - Firefox 70%, IE 30% as a start (aggregate all version 'times used' for the browsers). 我希望能够举例说 - Firefox 70%,IE 30%作为开始(聚合浏览器的所有版本'次'使用')。 I then want to drill down further to specific versions eg FF3.6 14%, FF14 51% etc. (simply printing out the times value associated with the version).
然后我想进一步深入到特定版本,例如FF3.6 14%,FF14 51%等(只需打印出与版本相关的时间值)。
EDIT : My Question is, how do I retrieve my data in the way described above? 编辑 :我的问题是,如何以上述方式检索我的数据? My head is fried and I've been on this for too long.
我的头很油炸,我已经这么久了。 Please someone put me out of my misery.
请有人让我摆脱痛苦。 If there is a better way to do this, please let me know also.
如果有更好的方法,请告诉我。
The code I have so far is below. 我到目前为止的代码如下。 This code is run in a for each loop which iterates through data returned by the database.
此代码在每个循环中运行,循环遍历数据库返回的数据。 The
$browser
and $version
variables are set to the browser values and version values returned from the database at each iteration of the foreach
loop. $browser
和$version
变量设置为在foreach
循环的每次迭代时从数据库返回的浏览器值和版本值。
if(in_array($browser, $uniqueBrowser)) {
if(in_array($version, $uniqueBrowser[$browser])) {
$uniqueBrowser[$browser][$version]['times'] = $uniqueBrowser[$browser][$version]['times'] + 1;
} else {
$uniqueBrowser[$browser]['version'] = $version;
$uniqueBrowser[$browser][$version]['times'] = 1;
}
} else {
$uniqueBrowser[] = $browser;
$uniqueBrowser[$browser]['version'] = $version;
$uniqueBrowser[$browser][$version]['times'] = 1;
}
When I run this code, it gives me this (via var_dump): 当我运行这段代码时,它给了我这个(通过var_dump):
Browser data: array(4) {
[0]=>
string(7) "Firefox"
["Firefox"]=>
array(3) {
["version"]=>
string(6) "14.0.1"
[15]=>
array(1) {
["times"]=>
int(2)
}
["14.0.1"]=>
array(1) {
["times"]=>
int(15)
}
}
[1]=>
string(17) "Internet Explorer"
["Internet Explorer"]=>
array(2) {
["version"]=>
string(9) "8.0.0.253"
["8.0.0.253"]=>
array(1) {
["times"]=>
int(1)
}
}
}
Instead of getting data out of the database, and put it in a complex array, you should let the database do the work for you, and retrieve the values from a query in the format that you want. 您应该让数据库为您完成工作,并以您想要的格式从查询中检索值,而不是从数据库中获取数据并将其放入复杂的数组中。
Let the database do the calculations, and do not use PHP to do that. 让数据库进行计算,不要使用PHP来做到这一点。 It makes it over complex.
它使它变得复杂。
I know JvdBerg has already given an answer like this, but I added my comment first. 我知道JvdBerg已经给出了这样的答案,但我先添加了我的评论。 So I don't feel like I'm copying him.
所以我不觉得我在抄袭他。
As an example I've created the following table structure. 作为一个例子,我创建了以下表结构。 Hopefully it's like yours.
希望它像你的一样。
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| browser | varchar(120) | YES | | NULL | |
| version | varchar(50) | YES | | NULL | |
+---------+------------------+------+-----+---------+----------------+
Then I inserted the following rows, again, hopefully like yours. 然后我再次插入以下行,希望和你的一样。
+----+---------+---------+
| id | browser | version |
+----+---------+---------+
| 1 | Firefox | 3.6 |
| 2 | Firefox | 4.1 |
| 3 | Firefox | 3.6 |
| 4 | Safari | 5.1.7 |
| 5 | Safari | 6 |
| 6 | Firefox | 14.0.1 |
| 7 | IE | 7 |
| 8 | IE | 8 |
| 9 | IE | 7 |
| 10 | Firefox | 14.0.1 |
| 11 | Opera | 12.0.1 |
| 12 | Safari | 5.1.7 |
+----+---------+---------+
Then the following query produced these results. 然后,以下查询生成了这些结果。
mysql> SELECT browser, version, COUNT(version) AS times FROM demo.browsers GROUP BY browser;
+---------+---------+-------+
| browser | version | times |
+---------+---------+-------+
| Firefox | 3.6 | 5 |
| IE | 7 | 3 |
| Opera | 12.0.1 | 1 |
| Safari | 5.1.7 | 3 |
+---------+---------+-------+
If you want it to be more precise you can use GROUP BY browser, version
instead of just by the browser. 如果您希望它更精确,您可以使用
GROUP BY browser, version
而不仅仅是浏览器。
mysql> SELECT browser, version, COUNT(version) AS times FROM demo.browsers GROUP BY browser, version;
+---------+---------+-------+
| browser | version | times |
+---------+---------+-------+
| Firefox | 14.0.1 | 2 |
| Firefox | 3.6 | 2 |
| Firefox | 4.1 | 1 |
| IE | 7 | 2 |
| IE | 8 | 1 |
| Opera | 12.0.1 | 1 |
| Safari | 5.1.7 | 2 |
| Safari | 6 | 1 |
+---------+---------+-------+
I'm not sure I fully understand your question, but have you tried iterating over the array using an extended for loop? 我不确定我是否完全理解你的问题,但你是否试过使用扩展的for循环迭代数组?
eg: 例如:
foreach ($uniqueBrowser as $browserName=>$values){
echo $browserName .' contains '. print_r($values,true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.