如何使用PHP数组创建PDF文件

I trying to create PDF using FPDF by PHP. Here is my code.

require 'fpdf.php';
$pdf= new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', "", 18);
$pdf->Ln();
$fruits=["apple", "Strawberry", "Banana"];
$animals=['Lion', 'Camel', 'Dog'];
$numbers=[1,2,3];

$i=0;
while($i<count($array))
{
    $pdf->Cell(5, 5, $fruits[$i]);

    $pdf->Cell(35, 35, $animals[$i]);
$pdf->Cell(35, 35, $numbers[$i]);
    $i++;
}
$pdf->output();

I am getting the output in this way

apple Strawberry Banana
Lion Camel Dog
1    2    3

But I am looking the output like with table format. I Have tried multiple ways using html tags like
, and regex ' '. But I didn't get output. I am getting the output in the above format only.

apple         Lion   1
Strawberry    Camel  2
Banana        Dog    3

Thanks in advance and welcome with all new ideas. Thanks You

First, $array is undefined in the condition of your while statement.

Your table is disarranged because you are adding cells with different heights.

This should work:

$i = 0;
while ($i < count($fruits))
{
    $pdf->Cell(35, 8, $fruits[$i]);
    $pdf->Cell(35, 8, $animals[$i]);
    $pdf->Cell(35, 8, $numbers[$i]);
    // Insert a line break at the end of each row.
    $pdf->Ln();
    $i++;
}

Am also use the same issues, I am using table

check my below code


     $pdf->AddPage();
        $pdf->SetFont('times','B',16);
        $htmlTable ="";
        while($i&gtcount($array)){
            $htmlTable +='&ltTABLE&gt
            &ltTR&gt
            &ltTD&gt'$fruits[$i].'&lt/TD&gt
            &ltTD&gt'.$animals[$i].'&lt/TD&gt
            &ltTD&gt'.$numbers[$i].'&lt/TD&gt
            &lt/TR&gt
            &lt/TABLE&gt';
            $pdf->WriteHTML2("&ltbr&gt".$htmlTable);
        }

In the above working perfectly.