'Explode' array then only display first element

时间:2015-09-14 16:15:55

标签: php

$value is a string of URLs. Each URL hotlinks to a JPG file and this code displays those on a page. Each URL is separated by a comma.

If I only want to display the first 1 or 2 images, what do I do? I tried to add a limiter to the explode function but I think I'm missing something because it doesn't quite work

$images = explode(',', $value);

$value = '';
foreach ($images as $im){
    $value .= '<img src="'. $im . '" />';
}

1 个答案:

答案 0 :(得分:4)

array_slice是你的朋友!

$images = array_slice(explode(',', $value), 0, 2);
//extracts 2 elements, starting with index 0
$value = '';
foreach ($images as $im){
    $value .= '<img src="'. $im . '" />';
}