希望你一切都好.我有一个查询,给我一个结果如下。
我需要一个多数组来显示它的数据化,就像这样:"Article, Products, Supname, Week1, Week2, Week3, Week4, Week5, Week6, and so1 Product 1 Supplier 1 0 0"。
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;
}
}
我试过很多不同的解决方案,但这是最接近的一个。 我缺少产品2的一周信息。这是可填充多达150个产品。
任何建议?
你可以像这样建立你的数组。
$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));