尝试创建超链接时未定义的通知

When trying to set the hyperlinks for each image name I am getting an undefined imgitem notice in the hyperlink(s). Am I placing the <a> tags in incorrect place?

echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
    echo '&nbsp;';
} else {
    echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[$imgitem]" target="_blank">';
    echo implode("</li>
<li></a><a href='previewImage.php?imgId=[$imgitem]' target='_blank'>", array_map(function($imgitem){
    return htmlspecialchars($imgitem);
},  $arrImageFile[$key]));
    echo '</li></ul></a>';
}
echo '</td>'; 

UPDATE:

Current code which is not working:

    <table id="tableqanda" cellpadding="0" cellspacing="0">
    <thead>
    <tr>
        <th width="11%" class="image">Image</th>
    </tr>
    </thead>
    </table>
    <div id="tableqanda_onthefly_container">
    <table id="tableqanda_onthefly" cellpadding="0" cellspacing="0">
    <tbody>
        <?php

function imageName( $imgitem ) {
    return htmlspecialchars($imgitem); //432
}


          foreach ($arrQuestionId as $key=>$question) {
        echo '<tr class="tableqandarow">'.PHP_EOL;
        echo '<td width="11%" class="imagetd">';

        if (empty($arrImageFile[$key])) {
            echo '&nbsp;';
        } else {
    echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">'; //line 456
    echo implode('</li>
<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) ); //line 457
    echo '</li></ul></a>';
        }
        echo '</td>';
        echo '</tr>'.PHP_EOL;
        }
?>
    </tbody>
    </table>
    </div>

Errors received:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in ... on line 432

Notice: Undefined variable: imgitem in ... on line 456

Notice: Undefined variable: imgitem in ... on line 457

I would change your structure to something like this which would work:

     function CreateLink($filename, $type){
 if($type == 'image'){
  return '<a href="previewimage.php?filename='.$filename.'" title="Click to view in New window" target="_blank">'.htmlspecialchars($filename).'</a>';
 }
}

......................


        $img_result = '';
        if(empty($arrImageFile[$key])){
          $img_result = '&nbsp;';
        }else{
          $img_result .=  '<ul class="qandaul">';
           if(is_array( $arrImageFile[$key] )){
            foreach($arrImageFile[$key] as $filename){
             $img_result.= '<li>' . CreateLink($filename, "image") . '</li>';
            }
           }else{
            $img_result.= CreateLink($arrImageFile[$key], "image");
           }
           $img_result.= '</ul>';
        }
        //end:procedure image

        echo '<td width="11%" class="imagetd">'.$img_result.'</td>' . PHP_EOL;

Using single quotes will return the literal value.

echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[$imgitem]" target="_blank">';

Change to:

echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">';

Update

function imageName( $imgitem ) {
    return htmlspecialchars($imgitem);
}

echo '<td width="11%" class="imagetd">';

if( empty($arrImageFile[$key]) ) {
    echo '&nbsp;';
} else {
    echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">'; //line 443
    echo implode('</li>
<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) );
    echo '</li></ul></a>';
}

echo '</td>';