0

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

1
  • Are the image URLs always at index 0? Commented Aug 3, 2015 at 21:01

2 Answers 2

1

If it is always the first value, you can simple access to the first (with index 0) string in nested array:

<?php foreach ($img_array AS $array) { ?>
            <div>
                <?php echo $array[0] ?>
            </div>
<?php } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick responses this is working great! I was using $array[$item ] instead of the key number.
0

Simply use for loop to do this:

$img_urls = array();
for ($i = 0; $i < count($img_array); $i++) {
    $img_urls[$i] = $img_array[$i][0]; // since it is always the first element in the array
}

echo $img_urls[1]; // this will show the first image url

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.