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 e.g. $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). I then want to drill down further to specific versions e.g. FF3.6 14%, FF14 51% etc. (simply printing out the times value associated with the version).
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.
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):
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)
}
}
}