我有Multi-Dimesional
我想重建这个数组,以便所有第一级数组(array[]
)具有另一个数组所缺少的数据(基本上替换丢失的数据)。我正在用这个数组构建一个图形,所以我需要用0
填充缺少的区域,否则我的图形中会有很大的漏洞。
[numberof]
是此给定情节的金额。
[date]
是它发生的月份。如你所见,我从今天的月份开始订购(3)ASC
:
当前输出
Array
(
[First] => Array
(
[0] => Array
(
[numberof] => 1
[date] => 12
)
[1] => Array
(
[numberof] => 2
[date] => 1
)
[2] => Array
(
[numberof] => 2
[date] => 2
)
[3] => Array
(
[numberof] => 25
[date] => 3
)
)
[Second] => Array
(
[0] => Array
(
[numberof] => 1
[date] => 1
)
[1] => Array
(
[numberof] => 2
[date] => 2
)
[2] => Array
(
[numberof] => 1
[date] => 3
)
)
[Third] => Array
(
[0] => Array
(
[numberof] => 1
[date] => 2
)
)
)
期望的结果
Array
(
[First] => Array
(
[0] => Array
(
[numberof] => 1
[date] => 12
)
[1] => Array
(
[numberof] => 2
[date] => 1
)
[2] => Array
(
[numberof] => 2
[date] => 2
)
[3] => Array
(
[numberof] => 25
[date] => 3
)
)
[Second] => Array
(
[0] => Array //ADDED and given a value of 0
(
[numberof] => 0
[date] => 12
)
[1] => Array
(
[numberof] => 1
[date] => 1
)
[2] => Array
(
[numberof] => 2
[date] => 2
)
[3] => Array
(
[numberof] => 1
[date] => 3
)
)
[Third] => Array
(
[0] => Array //ADDED and given a value of 0
(
[numberof] => 0
[date] => 12
)
[1] => Array //ADDED and given a value of 0
(
[numberof] => 0
[date] => 1
)
[2] => Array
(
[numberof] => 1
[date] => 2
)
[3] => Array //ADDED and given a value of 0
(
[numberof] => 0
[date] => 3
)
)
)
在我看来,这是不可能的。但也许我没有掌握技能,到目前为止我已经尝试过了:
n.b这是我大声说话 ....我无法理解如何做到这一点,循环遍历数组,检查它是否有值numbr,如果它没有添加它。但是,如果最后一行是月份最多且第一个月只有1个月呢?
修改
MySQL
:
$sql = sprintf("SELECT count(*) as numberOf, MONTH(s.sextime) as date FROM sex s, users u WHERE %s AND s.uid = u.uid AND s.sextime >= DATE_SUB(CURRENT_DATE, INTERVAL %d MONTH) GROUP BY MONTH(s.sextime) ORDER BY s.sextime ASC;", $type, $months);
这是PHP生成上述数组并调用sql:
$info['info']['First'] = $this->dataStore->grabSexesByMonth('AND s.sexnumber=1');
$info['info']['Second'] = $this->dataStore->grabSexesByMonth('AND s.sexnumber=2');
$info['info']['Third'] = $this->dataStore->grabSexesByMonth('AND s.sexnumber=3');
答案 0 :(得分:0)
三步流程可能有效:
$fillTo = array('key'=>'','count'=>0);
foreach($array as $k => $data) {
if (count($data) > $fillTo['count']) {
$fillTo['key'] = $k;
$fillTo['count'] = count($data);
}
}
$fillData = $array[$fillTo['key']];
foreach($fillData as $k => &$data) {
$data['numberof'] => 0;
}
foreach($array as $k => &$data) {
if ($k === $fillTo['key'])
continue;
$data = $data + $fillData;
}
var_dump($array);
答案 1 :(得分:0)
不幸的是MySQL
没有设置返回功能,例如PostgreSQL
generate_series。这使事情变得复杂,因为您需要以编程方式生成所有日期的集合。另外,您应该使用MONTH(sextime)
代替EXTRACT(YEAR_MONTH FROM sextime)
,否则您将获得不同年份的混合金额。
$months = 4;
# emulate GENERATE_SERIES
for ($i = $months-1; $i >= 0; $i--) {
$gen_series[] = "SELECT EXTRACT(YEAR_MONTH FROM CURRENT_DATE - INTERVAL $i MONTH) extr";
}
$gen_series = join(' UNION ', $gen_series);
$sql = sprintf("SELECT
fll.sexnumber,
fll.extr as date,
COUNT(u.uid) as numberOf
FROM
(SELECT
dts.extr,
sxn.sexnumber
FROM
(%s) dts,
(SELECT DISTINCT sexnumber FROM sex) sxn) fll LEFT JOIN sex s
ON
fll.extr = EXTRACT(YEAR_MONTH FROM s.sextime) AND fll.sexnumber=s.sexnumber LEFT JOIN users u
ON
s.uid = u.uid
GROUP BY
fll.sexnumber,
fll.extr
ORDER BY
fll.sexnumber,
fll.extr", $gen_series);
echo $sql;
上面的PHP代码将输出以下SQL查询。
SELECT
fll.sexnumber,
fll.extr as date,
COUNT(u.uid) as numberOf
FROM
(SELECT
dts.extr,
sxn.sexnumber
FROM
(SELECT EXTRACT(YEAR_MONTH FROM CURRENT_DATE - INTERVAL 3 MONTH) extr UNION SELECT EXTRACT(YEAR_MONTH FROM CURRENT_DATE - INTERVAL 2 MONTH) extr UNION SELECT EXTRACT(YEAR_MONTH FROM CURRENT_DATE - INTERVAL 1 MONTH) extr UNION SELECT EXTRACT(YEAR_MONTH FROM CURRENT_DATE - INTERVAL 0 MONTH) extr) dts,
(SELECT DISTINCT sexnumber FROM sex) sxn) fll LEFT JOIN sex s
ON
fll.extr = EXTRACT(YEAR_MONTH FROM s.sextime) AND fll.sexnumber=s.sexnumber LEFT JOIN users u
ON
s.uid = u.uid
GROUP BY
fll.sexnumber,
fll.extr
ORDER BY
fll.sexnumber,
fll.extr;
在同一数据集上运行它,您将从MySQL获得以下输出。
+-----------+--------+----------+
| sexnumber | date | numberOf |
+-----------+--------+----------+
| 1 | 201312 | 1 |
| 1 | 201401 | 2 |
| 1 | 201402 | 2 |
| 1 | 201403 | 25 |
| 2 | 201312 | 0 |
| 2 | 201401 | 1 |
| 2 | 201402 | 2 |
| 2 | 201403 | 1 |
| 3 | 201312 | 0 |
| 3 | 201401 | 0 |
| 3 | 201402 | 1 |
| 3 | 201403 | 0 |
+-----------+--------+----------+
12 rows in set (0.01 sec)
所以只需1个查询(而不是3个)就可以得到你需要的东西,而且没有多余的数组玩杂耍。