View the year and month that is coming from the array

Asked

Viewed 81 times

3

I can already display the year inside the foreach by position, as I do to display the month too?

The array is coming like this, the month in case would be that 3, 2, 3, 4 wanted to display this value inside my foreach (this is the var_dump in the variable files)

    array (size=2)
  2016 => 
    array (size=1)
      3 => 
        array (size=1)
          2 => 
            object(stdClass)[44]
              ...
  2017 => 
    array (size=3)
      2 => 
        array (size=1)
          1 => 
            object(stdClass)[45]
              ...
      3 => 
        array (size=1)
          3 => 
            object(stdClass)[46]
              ...
      4 => 
        array (size=1)
          2 => 
            object(stdClass)[47]
              ...

That’s my foreach, how could I do that?

<ul class="blogcat">
                        <?php foreach ($arquivos as $k => $a): ?>
                            <li>
                                <a href="#"><?= $k; ?></a>
                                <ul>
                                    <li><a href="#"><?= 'exibir o mes' ?></a></li>
                                </ul>
                            </li>
                        <?php endforeach; ?>
                    </ul>

1 answer

3


What you have is an array, a multi-dimensional array.

To resolve this issue you will need to nest one foreach inside another. It would be basically this:

<ul class="blogcat">
    <?php foreach ($arquivos as $indice1 => $valor1): ?>
        <li>
            <a href="#"><?= $indice1; ?></a>
            <ul>
                <?php foreach ($valor1 as $indice2 => $valor2): ?>
                    <li><a href="#"><?= $indice2; ?></a></li>
                <?php endforeach; ?>
            </ul>
        </li>
    <?php endforeach; ?>
</ul>

If you intend to display the days as well, just follow the same reasoning.

  • Puts it really, needed another foreach had forgotten me! Thanks Peter worked perfectly!

  • Thanks! I’m glad I helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.