希望你好。我有一个查询,结果如下:我需要这是一个多数组来将它显示在数据表中,如下所示:Article, Products, Supname, Week1, Week2, Week3, Week4, Week5, Week6, and so on1 Product 1 Supplier 1 0,27 0,6 0,36 0,58 0,78 0,32 and so on我用 PDO 得到了所有结果,因为我们使用的是 SQL 服务器,我创建数组的代码是这样的:$pos = 0;$x = 0; $End = count($getAll);$html = array();$html2 = array(); while ($x <= 53) { if ($x == 0) { $html[] = array('Articleno' => $getAll[$pos+1]["ArtNo"], 'Name' => ''.$getAll[$pos+1]["Product"], 'Supname' => $getAll[$pos+1]["Levnamn"]); } $html2 = array('M'.$getAll[$pos]["Weekno"] => $getAll[$pos]["Qty"].' , '.$getAll[$pos]["Sold"]); $html = array_merge($html, $html2); $x++; $pos++; if ($x == 53){ $x=0; } if ($pos==$End){ $x=70; } }结果还差得远,我尝试了很多不同的解决方案,但这是最接近的一个。我缺少 product2 的 alla 周信息。最多可填充 150 种产品。有什么建议 ?
1 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
您可以像这样构建数组:
$stm = $pdo->query('your SQL query');
$output = [];
while($row = $stm->fetch(PDO::FETCH_ASSOC))
{
$ident = $row['Articleno'].';'.$row['Product'].';'.$row['Supname'];
$ref = &$output[$ident];
if(!is_array($ref))
{
$ref['Article'] = $row['Articleno'];
$ref['Product'] = $row['Product'];
$ref['Supname'] = $row['Supname'];
$ref['Week1'] = [0,0];
$ref['Week2'] = [0,0];
$ref['Week3'] = [0,0];
$ref['Week4'] = [0,0];
$ref['Week5'] = [0,0];
$ref['Week6'] = [0,0];
}
$weekNo = $row['Weekno'];
$ref['Week'.$weekNo] = [$row['Qty'], $row['Sold']];
}
echo json_encode(array_values($output));
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报
0/150
提交
取消