Search code examples
phpphpword

PHPWord addListItem loop corrupts the document


$cInc = json_decode($inc);

$c = count((array)$cInc);

for ($x = 0; $x < $c; $x++)
{
    $section->addListItem($cInc[$x]);
}

So I want to loop the array $cInc to a List Item and somehow the loop corrupts the document.


Solution

  • i think you are wrong using count

    The count() function returns the number of elements in an array

    so you dont need array inside count

    just

    $cInc = json_decode($inc);
    
    $c = count($cInc);
    
    for ($x = 0; $x < $c; $x++)
    {
        $section->addListItem($cInc[$x]);
    }
    

    or u can use foreach if you dont know how many array that you have

    $cInc = json_decode($inc);
    
    foreach ($cInc as $val)
    {
        $section->addListItem($val);
    }