1

I'm trying to display an image on screen by reading the contents and array and using that value to fill in part of the file name.

In the code below the array has 3 values, 100, 101 and 102. I'm trying to take the first value "100" to output

If I just set $image to the value of "100" it works fine. When I set $image as an array_slice, and echo $image, it returns "array" instead of "100".

<?php

$dirname = "images/wrf/";
$array=array("100", "101", "102");
$image = array_slice($array, 0, 1);

echo '<img src="' . $dirname . 'wrf' . $image . 'medium.jpg">';

//out put for debugging 
print_r($array);
echo "contents of image===";
echo $image;

?>
1
  • php.net/array_slice array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) it clearly says that it returns an array. Commented Apr 18, 2016 at 16:10

2 Answers 2

4

array_slice result is array according to the docs,

if you'd like to get one element of array use [] operator instead

$image = $array[0]; 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did it.
Is there a way to increment the 0 in $image = $array[0]; ? I'd like to take each value in the array and display the image for each one.
perfect, that did it.
1
$image = array_slice($array, 0, 1);

output of $image

Array
(
    [0] => 100    
)

$image = $array[0];

output of $image

100

see edited code.

$dirname = "images/wrf/";
$array=array("100", "101", "102");
$image = $array[0];

echo '<img src="' . $dirname . 'wrf' . $image . 'medium.jpg">';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.