Jump to content

explode, array, for loop issue


cinek
 Share

Recommended Posts

I'm trying to save 3 values in 1 field in a text file. Each line in the textfile corresponds to a different div (different information). The data in the last field looks like this " 0 3 8". This should correspond to the image name (0.jpg etc). I'm trying to explode this by using:

//Explode slider array		$sliderContents[] = explode(" ", $sliderArray);

I then try to echo it so that every loop itteration has the correct value - from the text file

foreach ($sliderContents as $slider => $value){					echo '<img id="'.$directoryName.'-'.$slider.'" src="projects/'.$directoryName.'/'.$directoryName.'-'.$slider.'.jpg" alt="'.$directoryName.'-'.$slider.'" />';					//break;				}

but this does not work. All it does is, it runs once, twice, 3 times, 4 times, 5 times. It doesn't seem to be using the info from the text fileany ideas what's wrong?I tried using $value instead of slider - but its coming up as "Array" :)edit: if all I put in the field is 0 - to make the image 0.jpg appear - and try to print_r it, this is what I get:

Array ( [0] => [1] => 0 ) [3] => Array ( [0] => [1] => 0 ) [4] => Array ( [0] => [1] => 0 ) )

Edited by cinek
Link to comment
Share on other sites

$sliderContents[] = explode(" ", $sliderArray);

I think you're trying to create a single array and call it $sliderContents. In fact what is happening is $sliderContents is an array, but it does not contain the return value of explode. You have assigned that to the first element of $sliderContents. That's what the [] in $sliderContents[] do. Try removing the [] .

Edited by Deirdre's Dad
Link to comment
Share on other sites

$sliderContents[] = explode(" ", $sliderArray);

I think you're trying to create a single array and call it $sliderContents. In fact what is happening is $sliderContents is an array, but it does not contain the return value of explode. You have assigned that to the first element of $sliderContents. That's what the [] in $sliderContents[] do. Try removing the [] .

These [] symbols means position in an array left empty you can asign a new value, the array key will be int. You can also make create an array by just putting these symbols at the end of the variable instead of typing array(). Then using explode() the return value will be an array so you don't need to asign it to an array.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...