PDO获取第二列作为第二个数组的键

时间:2018-09-18 13:33:40

标签: php mysql pdo

我有一个SQL查询:

SELECT
    DATE,
    ID_DOMAIN,
    SUM(CLICKS) AS CLICKS,
    SUM(ADS) AS ADS,
    SUM(CONVERSION) AS CONVERSION,
    FROM website_stats
    WHERE ID_USER_PRODUCT_DIFFUSER = :id_user_product_diffuser
    AND DATE >= :date_begin
    AND DATE <= :date_end
    AND ID_DOMAIN != 0
    GROUP BY DATE, ID_DOMAIN;

在我的PDO fetchAll中,我使用$sth->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP)。然后我有这个输出:

array(2) {
  ["2018-09-14"]=>
  array(2) {
    [0]=>
    array(4) {
      ["ID_DOMAIN"]=>
      string(1) "7"
      ["CLICKS"]=>
      string(4) "1092"
      ["ADS"]=>
      string(4) "1070"
      ["CONVERSION"]=>
      string(3) "269"
    }
    [1]=>
    array(4) {
      ["ID_DOMAIN"]=>
      string(1) "9"
      ["CLICKS"]=>
      string(3) "459"
      ["ADS"]=>
      string(3) "452"
      ["CONVERSION"]=>
      string(2) "56"
    }
  }
  ["2018-09-15"]=>
  array(2) {
    [0]=>
    array(4) {
      ["ID_DOMAIN"]=>
      string(1) "7"
      ["CLICKS"]=>
      string(3) "914"
      ["ADS"]=>
      string(3) "880"
      ["CONVERSION"]=>
      string(3) "166"
    }
    [1]=>
    array(4) {
      ["ID_DOMAIN"]=>
      string(1) "9"
      ["CLICKS"]=>
      string(3) "365"
      ["ADS"]=>
      string(3) "361"
      ["CONVERSION"]=>
      string(2) "57"
    }
  }
}

但是对于我的代码,我需要ID_DOMAIN一个子数组中的键。有可能没有循环?我的意思是只用PDO::FETCH*吗?实际上,我需要一个像这样的数组:

array(2) {
  ["2018-09-14"]=>
  array(3) {
    [7]=>
    array(3) {
      ["CLICKS"]=>
      string(4) "1092"
      ["ADS"]=>
      string(4) "1070"
      ["CONVERSION"]=>
      string(3) "269"
    }
    [9]=>
    array(3) {
      ["ID_DOMAIN"]=>
      string(1) "9"
      ["CLICKS"]=>
      string(3) "459"
      ["ADS"]=>
      string(3) "452"
      ["CONVERSION"]=>
      string(2) "56"
    }
  }
  ["2018-09-15"]=>
  array(2) {
    [7]=>
    array(3) {
      ["ID_DOMAIN"]=>
      string(1) "7"
      ["CLICKS"]=>
      string(3) "914"
      ["ADS"]=>
      string(3) "880"
      ["CONVERSION"]=>
      string(3) "166"
    }
    [9]=>
    array(3) {
      ["CLICKS"]=>
      string(3) "365"
      ["ADS"]=>
      string(3) "361"
      ["CONVERSION"]=>
      string(2) "57"
    }
  }
}

我一直在寻找良好的PDO提取模式,但似乎没有人在此网站中遇到此问题:/

预先感谢您:)

0 个答案:

没有答案