A
A
advancesss2021-02-24 20:22:09
PHP
advancesss, 2021-02-24 20:22:09

How to output a two dimensional PHP array?

Greetings. Tell me how to display the [slider] array in such a two-dimensional construction:

Array
(
[slider] =>
images/about2.png
images/img3.png
images/news.png

)

Tried like this, it doesn't work

for ($i = 0; $i < count($additionalFields); $i++) {

     for ($k = 0; $k < count($additionalFields[$i]); $k++) {
     echo $additionalFields[$i][$k];
     }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sanya misharin, 2021-02-25
@advancesses

foreach($additionalFields as $slides) {
      foreach($slides as $slide) {
            echo $slide;
      }
}

Instead of curly ones, you can use : and endforeach in the layout.
For example, like this
<div class=“slider”>
<?php foreach($additionalFields as $slides) :
      foreach($slides as $slide) : ?>
            <div class=“slide”>
                    <img src=<?= $slide; ?>”/>
            </div> 
      <?php endforeach; ?>
<?php endforeach; ?>
</div>

If you just need to display the sliders array, then you can simply check that there is such a key and run foreach over it
if (isset($additionalFields[‘slider’])) {
      foreach($additionalFields[‘slider’] as $slide) {
            echo $slide;
      }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question