x87-Learner
x87-Learner

Reputation: 35

'Explode' array then only display first element

$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 . '" />';
}

Upvotes: 0

Views: 1143

Answers (1)

tillz
tillz

Reputation: 2108

array_slice is your friend!

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

Upvotes: 5

Related Questions