如何在 foreach 循环中内爆使用 array_push 的数组

发布于 2024-12-06 09:14:33 字数 732 浏览 3 评论 0原文

我的标签以正确的顺序显示在内部 foreach 循环中。

我想用逗号分隔它们,但不知道如何分隔。

有没有更好的方法来显示我的标签而不使用第二个 foreach 循环?

$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>";
}

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>";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-13 09:14:33

您不需要使用 array_push,因为您只向数组添加一个元素。您可以使用以下语法节省调用函数的开销:

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

我的答案取决于从数据库保存变量的必要性。在您提供的代码中,您仅在嵌套的 foreach 循环中使用数据库中的 id 和标记值。如果是这种情况,那么您可以简化数组,以便可以在新标签数组上使用 implode() 。我没有测试过这个,因为我没有你的数据库模式,但我相信它会起作用。

<?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 />';
}

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 />';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文