duanjingwei7239 2018-05-04 09:44
浏览 77

PHP数组和字符串

I have problems with arrays. When I try to get the first array [0] it does not give me anything. This is output

array(4) { [0]=> string(0) "" [333]=> string(123) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/f/de/d6/fded6f1587f863a9e8fc1c2173143a8782fa655e/700Wx700H-105395-0416.jpg" [334]=> string(125) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/e/b9/54/eb954216442547d2ed2c71adbcf73d4f2b3ef903/700Wx700H-105395-a-0416.jpg" [335]=> string(125) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/7/16/95/71695917dd17d29648c8f4907000e3c6cab64581/700Wx700H-105395-b-0416.jpg" }

and this is code

private function getImages($dom) {

        $images = [];

        foreach ($dom->getElementsByTagName('ul') as $ul) {
            if ($ul->getAttribute('class') == 'image-thumbnails') {
                foreach ($dom->getElementsByTagName('li') as $li) {
                    $images[] = $li->getAttribute('data-zoom-url');
                }
            }
        }

        $images = array_unique($images);

        return $images;
    }
  • 写回答

1条回答 默认 最新

  • dplece1882 2018-05-04 09:48
    关注

    If you want to exclude empty values, check if the attribute is empty before adding it to the array:

    if(!empty($li->getAttribute('data-zoom-url'))) {
       $images[] = $li->getAttribute('data-zoom-url');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用