创建多数组以在数据表 PDO、PHP 中显示

希望你好。我有一个查询,结果如下:

http://img2.mukewang.com/64704f290001662204910540.jpg

我需要这是一个多数组来将它显示在数据表中,如下所示:


Article, Products,  Supname,    Week1, Week2, Week3, Week4, Week5, Week6, and so on

1         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 种产品。


有什么建议 ?


SMILET
浏览 145回答 1
1回答

慕盖茨4494581

您可以像这样构建数组:$stm = $pdo->query('your SQL query');$output = [];while($row = $stm->fetch(PDO::FETCH_ASSOC)){&nbsp; $ident = $row['Articleno'].';'.$row['Product'].';'.$row['Supname'];&nbsp; $ref = &$output[$ident];&nbsp; if(!is_array($ref))&nbsp; {&nbsp; &nbsp; $ref['Article'] = $row['Articleno'];&nbsp; &nbsp; $ref['Product'] = $row['Product'];&nbsp; &nbsp; $ref['Supname'] = $row['Supname'];&nbsp; &nbsp; $ref['Week1'] = [0,0];&nbsp; &nbsp; $ref['Week2'] = [0,0];&nbsp; &nbsp; $ref['Week3'] = [0,0];&nbsp; &nbsp; $ref['Week4'] = [0,0];&nbsp; &nbsp; $ref['Week5'] = [0,0];&nbsp; &nbsp; $ref['Week6'] = [0,0];&nbsp; }&nbsp; $weekNo = $row['Weekno'];&nbsp; $ref['Week'.$weekNo] = [$row['Qty'], $row['Sold']];}echo json_encode(array_values($output));
打开App,查看更多内容
随时随地看视频慕课网APP