[英]multidimentional array printing in a php class
有一個FPDF類,我在類中傳遞一個數組,其名稱為productList 。
$myclass->productList($table_rows);
該數組包含MySql表中的一些行
喜歡
Array (
[0] => Array ( [id] => 170 [product] => 10211 [qty] => 1 [price] => 50 )
[1] => Array ( [id] => 171 [product] => 10211 [qty] => 1 [price] => 50 ) 1
我正在使用一個額外的函數來解析數組,但它只顯示第一行。 另外,它顯示帶有數據的表列名稱
例如id1,data1 ## somedata
我想要的輸出是
1,#somedata,#some 2,#somedata,#some
期望的輸出圖像:
當前輸出圖像:
首先我發布數組打印功能
function makeNestedList(array $Array){
$w = array(90, 25, 20, 25,30);
foreach($Array as $Key => $Value){
$Output = $Key;
if(is_array($Value)){
$Output .= $this->makeNestedList($Value);
}else{
$Output .= $Value;
}
//Product List
$this->Cell($w[0],6,$Output,'LR',0,'L',1);
}
return $Output;
}
以及負責打印的產品清單功能
function productList($pdata)
{
//Function to print product table
// Product Table Header
$header = array('Product', 'Qty', 'Rate', 'Discount','Subtotal');
//cell width $w
$w = array(90, 25, 20, 25,30);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
// Product Data <===== Here is problem
$this->makeNestedList($pdata);
// Closing line and extra
$this->Cell(array_sum($w),0,'','T');
$this->Ln();
}
經過長時間艱苦的嘗試,我得到了這個簡單的解決方案
//get $pdata
$this->productList($tablerows);
function productList($pdata)
{
and set a loop
foreach($table_rows as $key => $value)
{
$this->Cell($w[0],6,$value['product'],'LR',0,'L',$fill);
$this->Cell($w[1],6,$value['qty'],'LR',0,'C',$fill);
$this->Cell($w[2],6,$value['price'],'LR',0,'C',$fill);
$this->Cell($w[3],6,$value['discount'],'LR',0,'C',$fill);
$this->Cell($w[4],6,$value['subtotal'],'LR',0,'C',$fill);
$this->Ln();
$fill = !$fill;
}
如果您的數組數據如下所示:
array (
array ( 'product' => 10211, 'qty' => 1, 'rate' => 50, 'discount' => 50 , 'subtotal' => 50 ),
array ( 'product' => 10212, 'qty' => 1, 'rate' => 100, 'discount' => 100 , 'subtotal' => 100 )
);
該代碼可能對您有用。 我看到您將數組的所有值連接到$Output
並放入一個單元格中。 如果要生成表,則不應該這樣做。
function makeNestedList(array $Array){
$w = array(90, 25, 20, 25,30);
for ($i=0; $i < count($Array) ; $i++) {
$ii = 0;
foreach ($Array[$i] as $key => $value) {
$this->Cell($w[$ii],6,$Output,'LR',0,'L',1);
$ii++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.