从PHP多维数组检索值 [英] Retrieving values from a PHP Multi-dimensional Array

查看:137
本文介绍了从PHP多维数组检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,我想从一个网站管理员打印统计多维数组。我的问题是,一旦我得到的数据到我想要的格式的数组,我无法弄清楚如何找回它。我必须知道键值为了得到它例如 $ uniqueBrowser [火狐] [14.0.1] ['时代'];

我要能够说 - 例如Firefox的70%,即30%作为启动(总所有版本的时间使用的浏览器)。然后,我想进一步例如向下钻取到特定版本FF3.6 14%,FF14 51%等。(简单地打印出与版本相关的时间值)。

修改:我的问题是,我该如何找回在上述的方式我的数据?
我的头被油炸,我已经对这个太久。请有人把我从我的痛苦。如果有更好的方式来做到这一点,请让我也知道。

在code我到目前为止如下。这code是一个为它通过数据迭代由数据库返回的每个循环中运行。在 $浏览器 $版本变量设置为浏览器的价值和版本价值从数据库在每次迭代返回在的foreach 循环。

 如果(in_array($浏览器,$ uniqueBrowser)){
    如果(in_array($版本,$ uniqueBrowser [$浏览器])){
        $ uniqueBrowser [$浏览器] [$版本] ['时代'] = $ uniqueBrowser [$浏览器] [$版本] ['时代'] + 1;
    }其他{
        $ uniqueBrowser [$浏览器] ['版本'] = $版本;
        $ uniqueBrowser [$浏览器] [$版本] ['时代'] = 1;
    }
}其他{
    $ uniqueBrowser [] = $浏览器;
    $ uniqueBrowser [$浏览器] ['版本'] = $版本;
    $ uniqueBrowser [$浏览器] [$版本] ['时代'] = 1;
}

当我运行这个code,它给我这个(通过的var_dump):

 浏览器数据:阵列(4){
  [0] =>
  字符串(7)火狐
  [火狐] =>
  阵列(3){
    [版本] =>
    串(6)14.0.1
    [15] =>
    阵列(1){
      [时代] =>
      INT(2)
    }
    [14.0.1] =>
    阵列(1){
      [时代] =>
      INT(15)
    }
  }
  [1] =>
  串(17)的Internet Explorer
  [的Internet Explorer] =>
  阵列(2){
    [版本] =>
    串(9)8.0.0.253
    [8.0.0.253] =>
    阵列(1){
      [时代] =>
      INT(1)
    }
  }
}


解决方案

我知道 JvdBerg 已经给出的答案一样这一点,但我第一次加入我的评论。所以我不觉得我抄袭他。

作为一个例子,我创建了下面的表结构。希望它像你。

  + --------- + ------------------ + ------ +  - ---- + --------- + ---------------- +
|现场|类型|空|钥匙|默认|额外|
+ --------- + ------------------ + ------ + ----- + ------- - + ---------------- +
| ID | INT(11)无符号| NO | PRI | NULL | AUTO_INCREMENT |
|浏览器| VARCHAR(120)| YES | | NULL | |
|版| VARCHAR(50)| YES | | NULL | |
+ --------- + ------------------ + ------ + ----- + ------- - + ---------------- +

然后我插入下面的行,请再次,希望像你这样的。

  + ---- + --------- + --------- +
| ID |浏览器|版|
+ ---- + --------- + --------- +
| 1 |火狐| 3.6 |
| 2 |火狐| 4.1 |
| 3 |火狐| 3.6 |
| 4 | Safari浏览器| 5.1.7 |
| 5 | Safari浏览器| 6 |
| 6 |火狐| 14.0.1 |
| 7 | IE浏览器| 7 |
| 8 | IE浏览器| 8 |
| 9 | IE浏览器| 7 |
| 10 |火狐| 14.0.1 |
| 11 |歌剧| 12.0.1 |
| 12 | Safari浏览器| 5.1.7 |
+ ---- + --------- + --------- +

那么下面的查询产生这些结果。

  MySQL的>选择浏览器,版本,COUNT(稿)时间从demo.browsers GROUP BY浏览器;
+ --------- + --------- + ------- +
|浏览器|版|次|
+ --------- + --------- + ------- +
|火狐| 3.6 | 5 |
| IE浏览器| 7 | 3 |
|歌剧| 12.0.1 | 1 |
| Safari浏览器| 5.1.7 | 3 |
+ --------- + --------- + ------- +

如果您希望它是更precise可以使用 GROUP BY浏览器,版本,而不仅仅是浏览器。

  MySQL的>选择浏览器,版本,COUNT(稿)时间从demo.browsers GROUP BY浏览器,版本;
+ --------- + --------- + ------- +
|浏览器|版|次|
+ --------- + --------- + ------- +
|火狐| 14.0.1 | 2 |
|火狐| 3.6 | 2 |
|火狐| 4.1 | 1 |
| IE浏览器| 7 | 2 |
| IE浏览器| 8 | 1 |
|歌剧| 12.0.1 | 1 |
| Safari浏览器| 5.1.7 | 2 |
| Safari浏览器| 6 | 1 |
+ --------- + --------- + ------- +

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)
    }
  }
}

解决方案

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 |
+---------+---------+-------+

这篇关于从PHP多维数组检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆