从foreach循环输出中排除值

时间:2016-11-28 20:09:19

标签: php arrays foreach

我希望从我的foreach输出中排除friend_onefriend_two中出现的特定值。我不想从我的查询中排除这一点。

要排除的参数为$profile_user,它出现在friend_onefriend_two中。我试图执行此操作if(in_array($friend_total_row->friend_one == $profile_user)),但我收到错误,而且只有friend_one

任何人都知道我该怎么做?

<?php           
    //Friends --- total
    $friend_status = 2;
    $friend_sql = "
        SELECT *
        FROM friends
        WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status
    ";
    $friend_stmt = $con->prepare($friend_sql);
    $friend_stmt->execute(array(':profile_user' => $profile_user, ':total_status' => $friend_status));
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    $count_total_friend = $friend_stmt->rowCount();
?>  
        <div id="friend-list-container">
            <div id="friend-list-count">Friends <span class="light-gray"><?php echo $count_total_friend; ?></span></div>
            <div id="friend-list-image-container">
<?php           
    foreach ($friend_total_rows as $friend_total_row) {
        if(in_array($friend_total_row->friend_one == $profile_user)) {
            continue;
        }
        else {
        $friend_1   = $friend_total_row['friend_one'];
        $friend_2   = $friend_total_row['friend_two'];
        //$friend_status        = $friend_total_row['status'];
        //$friend_status_date = $friend_total_row['date'];
        }
        echo $friend_1 . $friend_2;
    }
?>

我只想将$profile_user排除在输出之外,这就是X所在的位置。然后我想要平方输出显示。 enter image description here

1 个答案:

答案 0 :(得分:1)

更新回答:

b