Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6608943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:39:48+00:00 2026-05-25T19:39:48+00:00

My tags are showing in the inner foreach loop in the right order. I

  • 0

My tags are showing in the inner foreach loop in the right order.

I would like to comma separate them but am not sure how.

Is there a better way to display my tags without using the second foreach loop?

$people = array();

while($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)){
       if(!isset($people[$row["id"]])){
            $people[$row["id"]]["id"] = $row["id"];
            $people[$row["id"]]["tag"] = $row["tag"];
            $people[$row["id"]]["tags"] = array();
       }
     array_push($people[$row["id"]]["tags"],  array("id"=>$row["tags_id"],"tag_name"=>$row["tag"]));
}

foreach($people as $pid=>$p){

    echo "(#{$p['id']}) ";

     foreach($p["tags"] as $tid=>$t){
     echo "<a href='#'>{$t['tag_name']}</a> ";   
     }

       echo "<br><br>";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:39:48+00:00Added an answer on May 25, 2026 at 7:39 pm

    You don’t need to use array_push, since you’re only adding one element to the array. You can save the overhead of calling a function by using the syntax:

    $people[ $row["id"] ]["tags"][] = array(...);
    

    My answer depends on the necessity of the variables saved from the database. In your supplied code, you’re only using the id and tag values from the database in the nested foreach loops. If this is this case, then you can simplify your arrays so you can use implode() on a new tags array. I have not tested this since I do not have your database schema, but I believe it will work.

    <?php
    $people = array();
    $tags = array();
    
    while( ($row = mysqli_fetch_array( $rs, MYSQLI_ASSOC)))
    {
        if( !isset( $people[$row['id']]))
        {
            $people[ $row['id'] ] = array( 'id' => $row['id'], 'tag' => $row['tag']);
            $tags[ $row['id'] ] = array();
        }
        $tags[ $row['id'] ][] = $row['tag'];
    }
    
    foreach( $people as $pid => $p)
    {
        echo "(#{$p['id']}) ";
        echo '<a href="#">' . implode( '</a><a href="#">', $tags[ $p['id'] ])  . '</a>';
        echo '<br /><br />';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The pound signs (£) &pound; work perfectly within <p> tags, but are not showing
I've added the required tags, but it is still not showing. I've added this
I'm able to display id and tags in the foreach loop no problem. But
My ITagger<IClassificationTag> is generating tags, however, Visual Studio is not showing the syntax coloring.
The tags might not be accurate since I am not sure where the problem
I'm using IntellIJ with Apache Wicket and IntelliJ is showing me that tags like
I'm not sure how to display tags in my view that belong to a
I have multiple <select/> tags i would like to synchronize so that when one
Gitk has a nice habit of showing me Tags:, Follows: and Precedes: for commit.
I'm encountering an odd problem showing .svg using simple img tags on Apache. On

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.