dqkf36241 2012-09-07 12:24
浏览 40
已采纳

从PHP多维数组中检索值

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)
    }
  }
}
  • 写回答

3条回答 默认 最新

  • drv16821 2012-09-07 12:44
    关注

    I know JvdBerg has already given an answer like this, but I added my comment first. 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.

    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 |
    +---------+---------+-------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口