简体   繁体   中英

PHP arrray and string

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;
    }

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM