Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在img数组中动态合并alt标记数组_Javascript_Php_Arrays - Fatal编程技术网

Javascript 在img数组中动态合并alt标记数组

Javascript 在img数组中动态合并alt标记数组,javascript,php,arrays,Javascript,Php,Arrays,我有一个alt标记数组: $array_alt=array("hii, "Best", "abc", //"board certified", "xyz", "hello", "new" ) 现在我想动态地将这些alt标记添加到img src,也就是说,对于image 1alt标记应该是“hii”,其余的都是一样的 这就是通过文

我有一个alt标记数组:

$array_alt=array("hii,
            "Best",
            "abc",
            //"board certified",
            "xyz",
            "hello",
            "new"
            )
现在我想动态地将这些alt标记添加到img src,也就是说,对于image 1alt标记应该是“hii”,其余的都是一样的

这就是通过文件夹动态调用图像的方式,所有图像都显示在滑块中:

$images = scandir(images/all_images); // get path 
            sort($images,1); // 1 is to sort images numerically

            foreach($images as $img)
                { 
                    if($img === '.' || $img === '..')
                    {
                        continue;
                    }   
                    // check extensions as we need only images
                    if ((preg_match('/.jpg/',$img))  ||  (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
                    {               
                        list($width, $height, $type, $attr) = getimagesize($path.$img);

                        if(($width<$height) || ($height>500) )
                        {
                        ?>
                            <div class="item img-landscape"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                        else{
                        ?>
                            <div class="item"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
$images=scandir(图像/所有图像);//获取路径
排序($images,1);//1是对图像进行数字排序
foreach($img形式的图像)
{ 
如果($img=='.| |$img==='.')
{
继续;
}   
//检查扩展,因为我们只需要图像
if((preg_-match('/.jpg/,$img))|(preg_-match('/.gif/,$img))|(preg_-match('/.tiff/,$img))|(preg_-match('/.png/,$img)))
{               
列表($width、$height、$type、$attr)=getimagesize($path.$img);
如有($500))
{
?>
" >
" >
" >
" >

首先,你的声明是错误的

$array_alt=array("hii,
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        )
会是

$array_alt=array("hii",
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        )
修改了一点您的代码(在alts数组中的hii
之后添加缺少的
,检查
$array\U alt
中是否存在密钥,简化扩展检查),这似乎对我有效:

<?php

$array_alt = array(
    "hii",
    "Best",
    "abc",
    "board certified",
    "xyz",
    "hello",
    "new"
);

$path = '/public_path/';
$images = scandir($img_path);
sort($images, SORT_NUMERIC); // SORT_NUMERIC == 1 is to sort images numerically

$allowedExtensions = array('jpg', 'gif', 'tiff', 'png');
$i = 0;
foreach ($images as $img) {
    if (isset($array_alt[$i])) {
        $alt = $array_alt[$i];
    } else {
        $alt = "default_alt_value";
    }
    echo $alt . "<br>";
    if ($img === '.' || $img === '..') {
        continue;
    }
// check extensions as we need only images
    if (!in_array(pathinfo($img, PATHINFO_EXTENSION), $allowedExtensions)) {
        continue;
    }

    list($width, $height, $type, $attr) = getimagesize($path . $img);

    if (($width < $height) || ($height > 500)) {
        ?>
        <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $alt ?>"
                                             data-src="<?php echo $path . $img; ?>"></div>
    <?php
    } else {
        ?>
        <div class="item"><img class="lazyOwl" alt="<?php echo $alt ?>"
                               data-src="<?php echo $path . $img; ?>"></div>
    <?php
    }
    $i++;
}
?>

">
">
$array\u alt=array(“hii”,
“最佳”,
“abc”,
//“董事会认证”,
“xyz”,
“你好”,
“新的”
);
$img_path=“图像/滑块”;
$path=JURI::BASE().$img_path.“/”;//图像的url/路径
$images=scandir($img_路径);
排序($images,1);//1是对图像进行数字排序
$i=0;
foreach($img形式的图像)
{ 
如果($img=='.| |$img==='.')
{
继续;
}   
//检查扩展,因为我们只需要图像
if((preg_-match('/.jpg/,$img))|(preg_-match('/.gif/,$img))|(preg_-match('/.tiff/,$img))|(preg_-match('/.png/,$img)))
{               
列表($width、$height、$type、$attr)=getimagesize($path.$img);
如有($500))
{
?>
" >
" >

为什么使用JavaScript和jQuery标记?代码显示了什么错误?alt标记没有更改吗?问题是什么?欢迎提供更多信息:)如果可以使用JavaScript或jQuery完成此操作,这就是为什么还要添加这些标记。我尝试过的代码多次打印相同的alt标记,这是没有用的。我希望每个图像都应该拥有数组中定义的自己的alt标记您确定匹配规则正确吗?如果您多次说存在相同的alt标记,我唯一能了解代码不起作用的原因是您的代码太频繁地到达continue;语句。是一样的,原因是什么difference@user3480318事实上,不是同样。你忘了在第一个字符串的末尾加一个Yes,这解决了我的问题。我的代码运行正常
<?php

$array_alt = array(
    "hii",
    "Best",
    "abc",
    "board certified",
    "xyz",
    "hello",
    "new"
);

$path = '/public_path/';
$images = scandir($img_path);
sort($images, SORT_NUMERIC); // SORT_NUMERIC == 1 is to sort images numerically

$allowedExtensions = array('jpg', 'gif', 'tiff', 'png');
$i = 0;
foreach ($images as $img) {
    if (isset($array_alt[$i])) {
        $alt = $array_alt[$i];
    } else {
        $alt = "default_alt_value";
    }
    echo $alt . "<br>";
    if ($img === '.' || $img === '..') {
        continue;
    }
// check extensions as we need only images
    if (!in_array(pathinfo($img, PATHINFO_EXTENSION), $allowedExtensions)) {
        continue;
    }

    list($width, $height, $type, $attr) = getimagesize($path . $img);

    if (($width < $height) || ($height > 500)) {
        ?>
        <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $alt ?>"
                                             data-src="<?php echo $path . $img; ?>"></div>
    <?php
    } else {
        ?>
        <div class="item"><img class="lazyOwl" alt="<?php echo $alt ?>"
                               data-src="<?php echo $path . $img; ?>"></div>
    <?php
    }
    $i++;
}
?>
$array_alt=array("hii",
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        );

$img_path="images/slider";
$path=JURI::BASE().$img_path."/"; // url/path of the images
$images = scandir($img_path);
sort($images,1); // 1 is to sort images numerically

    $i=0;
    foreach($images as $img)
    { 
       if($img === '.' || $img === '..')
       {
           continue;
       }   
       // check extensions as we need only images
       if ((preg_match('/.jpg/',$img))  ||  (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )

                {               
                    list($width, $height, $type, $attr) = getimagesize($path.$img);

                    if(($width<$height) || ($height>500) )
                    {
                    ?>
                        <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                       <?php
                    }
                    else{
                    ?>
                        <div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                       <?php
                    }
                }
                else
                {
                    continue;
                }
               $i++;  
            }