0

for each Example 1 caption and 3 occurrences for the source map

What am I doing wrong?

Images Array

$imgsArray = array(
  'image1-small.jpg', 'image1-medium.jpg', 'image1-large.jpg',
  'image2-small.jpg', 'image2-medium.jpg', 'image2-large.jpg',
  'image3-small.jpg', 'image3-medium.jpg', 'image3-large.jpg'
); // sometimes more pictures too

Captions Array

$imgCaption = array('Adam','Peter','Susi');

the code

foreach($imgsArray as $files => $img) {
  $first_char = substr($img,0,strpos($img,"-")); // delete from hyphen to the last string
  if ($first_char != $last_entry) {
  echo '<p>This is '.$imgCaption[$files];
  echo '<ul>';
    echo '<li>'.$first_char.'-small.jpg</li>';
    echo '<li>'.$first_char.'-medium.jpg</li>';
    echo '<li>'.$first_char.'-large.jpg</li>';
    echo '<li>'.$imgCaption[$files].'</li>';
  echo '</ul>';
  echo '<i>Ciao, '.$imgCaption[$files].'</i><br>---</p>';
  }
  $last_entry = $first_char;
}

unexpected

This is Adam

  • image1-small.jpg
  • image1-medium.jpg
  • image1-large.jpg
  • Adam
Ciao, Adam
---

This is Notice: Undefined offset: 3 in...

  • image2-small.jpg
  • image2-medium.jpg
  • image2-large.jpg
  • Notice: Undefined offset: 3 in...
Ciao, Notice: Undefined offset: 3 in...
---

This is Notice: Undefined offset: 3 in...

  • image3-small.jpg
  • image3-medium.jpg
  • image3-large.jpg
  • Notice: Undefined offset: 6 in...
Ciao, Notice: Undefined offset: 3 in...

---

expected

This is Adam

  • image1-small.jpg
  • image1-medium.jpg
  • image1-large.jpg
  • Adam
Ciao, Adam
---

This is Peter

  • image2-small.jpg
  • image2-medium.jpg
  • image2-large.jpg
  • Peter
Ciao, Peter
---

This is Susi

  • image3-small.jpg
  • image3-medium.jpg
  • image3-large.jpg
  • Susi
Ciao, Susi
---

2
  • Something just to check - why do you have all the values in $imgsArray and then ignore most of them and add what looks like the same extension in the loop? Commented Apr 11, 2020 at 7:53
  • I have simplified the question. the image groups are actually under <img> tags (depending on the resolution) and each image group also has caption Commented Apr 11, 2020 at 8:06

2 Answers 2

1

The problem is that using the index to the $imgsArray for the $imgCaption array isn't going to work.

The easy fix is to just keep track of how many times you put out the text and increment it each time ($textCount in this example) ...

$textCount = 0;
$last_entry = '';
foreach($imgsArray as $files => $img) {
    $first_char = substr($img,0,strpos($img,"-")); // delete from hyphen to the last string
    if ($first_char != $last_entry) {
        echo '<ul>';
        echo '<li>'.$first_char.'-small.jpg</li>';
        echo '<li>'.$first_char.'-medium.jpg</li>';
        echo '<li>'.$first_char.'-large.jpg</li>';
        echo '<li>'.$imgCaption[$textCount++].'</li>';
        echo '</ul>';
    }
    $last_entry = $first_char;
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, but to avoid abuses I took $imgArray names instead of numbers - $imgCaption = array('Adam','Peter','Susi');
OK, not sure what difference that makes, this code just move on to the next element in $imgCaption each time the name changes in $imgArray.
You're right! Thank you for your answer. Best regards from Vienna
I have simplified the question. the image groups are actually under <img> tags (depending on the resolution) and each image group also has caption
1

If you always have one caption for 3 images you might want to consider a numerically indexed loop:

$imgsArray = array(
  'image1-small.jpg', 'image1-medium.jpg', 'image1-large.jpg',
  'image2-small.jpg', 'image2-medium.jpg', 'image2-large.jpg',
  'image3-small.jpg', 'image3-medium.jpg', 'image3-large.jpg'
);

$imgCaption = array('Adam','Peter','Susi');

for ($i = 0; $i < count($imgCaption); $i++) {
    echo '<p>This is '.$imgCaption[$i];
    echo "<ul>\n";
    for ($j = 0; $j < 3; $j++) {
        echo '<li>' . $imgsArray[$i * 3 + $j] . "</li>\n";
    }
    echo "<li>{$imgCaption[$i]}</li>\n";
    echo "</ul>\n";
    echo '<i>Ciao, '.$imgCaption[$i].'</i><br>---</p>';
}

Output:

This is Adam

  • image1-small.jpg
  • image1-medium.jpg
  • image1-large.jpg
  • Adam
Ciao, Adam
---

This is Peter

  • image2-small.jpg
  • image2-medium.jpg
  • image2-large.jpg
  • Peter
Ciao, Peter
---

This is Susi

  • image3-small.jpg
  • image3-medium.jpg
  • image3-large.jpg
  • Susi
Ciao, Susi
---

Demo on 3v4l.org

4 Comments

I have simplified the question. the image groups are actually under <img> tags (depending on the resolution) and each image group also has caption
We can only answer the question you have posted. That calls for unordered lists, which this (and Nigel's answer) produce. I've updated my answer to reflect your expected output
Thank you for your answer. Best regards from Vienna!
@CemFirat not sure what you mean by that. This code is simply outputting the image names that exist in the array. I think it would be good if you updated the question to better reflect your actual code, it would make it a lot easier to give you a solution that you can then make work in your environment.

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.