0

Im working on a custom code right now and I get this warning about an invalid argument for foreach()

Here is my code below and the line 61 is the tags as tag:

<h3 class="mt-5 mb-3 h2 font-weight-light text-center">Categories</h3>

                        <div class="grid-columns">
                            <?php foreach( $page_context->tags->items as $item ) :
                                foreach( $item as $tags ) :
                                    if(isset($tags)):
                                        echo('<div style=\'display: none;\'>');
                                        echo ($tags);
                                        echo('</div>');
                                    foreach( $tags as $tag ) : ?>

                                      
                                        <button class="card library-tag" data-id="<?= $tag->id; ?>">
                                            <a class="disabled" aria-label="<?= $tag->name ?>" href="/catalog/#category-<?= $tag->id; ?>">
                                            <div class="panel-box shadow-box">
                                                <img class="card-img-top" src="<?= $tag->thumbnailPath ?>" alt="<?= $tag->name ?>a">
                                                <div class="card-body border-top">
                                                    <h6 class="card-title"><?= $tag->name ?></h6>
                                                </div>
                                            </div>
                                            </a>
                                        </button>
                              <?php endforeach;
                                    endif;
                                endforeach;
                            endforeach; ?>
                        </div>
                </div>
            </section>
Val Ong
  • 3
  • 3
  • Could it be because my array is empty? – Val Ong Sep 12 '22 at 18:15
  • If you can echo `$tags` 2 lines above it, then `$tags` is not something you can iterate through. It's a simple value like a string or number, not an array. – aynber Sep 12 '22 at 18:22

1 Answers1

-1

Add these three lines of code to troubleshoot.
You see why the foreach does not work

<?php foreach( $page_context->tags->items as $item ) :
    foreach( $item as $tags ) :
        if(isset($tags)):
            echo('<div style=\'display: none;\'>');
            echo ($tags);
            echo('</div>');
            
            echo '<pre>';
            var_export($tags);
            exit;
            
        foreach( $tags as $tag ) : ?>
Misunderstood
  • 5,534
  • 1
  • 18
  • 25