OK I'm new to arrays and this is probably simple but I am having a hard time getting my head around it.
I have a simple nested array. What I would like to do is loop through all the values get only the img url's and store them in a separate array of variables.
My current code works to find the urls but is not very clean and does not target the url entry of the array but simply tests to see if the variable starts with "http" to determine if it is the correct entry. I would like to simply create a variable from the first item of each nested array.
Currently array and code is structured as follows:
Array
(
[0] => Array
(
[0] => http://samplesite.com/img1.jpg
[1] => 1000
[2] => 667
[3] =>
)
[1] => Array
(
[0] => http://samplesite.com/img2.jpg
[1] => 1000
[2] => 667
[3] =>
)
)
<?php foreach ($img_array AS $array) {
foreach ($array AS $item) {
//check to see if it is the URL entry else skip that part of array
if (strpos($item, "http") === 0) {;?>
<div>
<?php echo $item ?>
</div>
<?php
}
}
}?>
I know you can target the nested arrays with this kind of statement:
foreach ($img_array AS $array => $item)
However when I use this I don't return any values just the word "array"?
Since these are always the first value of the nested array I should be able to bypass using the if statement to look for "http" to determine if the value is a url.
The goal is to extract all the urls as unique variables that could be used to generate a slide show from the images.
Something like this. Where the foreach loop generates the individual slide code.
<div class="item active">
<img src="<?php $array[0] ?>" alt="alt" width="1000" height="400">
<div class="carousel-caption">
<p>Caption text here</p>
</div>
</div>
<div class="item">
<img src="<?php $array[1] ?>" alt="alt" width="1000" height="400">
<div class="carousel-caption">
<p>Caption text here</p>
</div>
</div>
<div class="item">
<img src="<?php $array[2] ?>" alt="alt" width="1000" height="400">
<div class="carousel-caption">
<p>Caption text here</p>
</div>
</div>
Thanks for looking!
Eric