How to get the id of each object from a php foreach loop?
<p>Using a foreach loop in php I'm trying to retrieve data from a database. The idea is to display data, such as a title, from a database using a foreach loop in order to get the data into objects / divs.</p>

<p>Here is the example:
<a href="https://i.stack.imgur.com/N7ws9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N7ws9.jpg" alt="result example"></a></p>

<p>This is the database (database->posts):
<a href="https://i.stack.imgur.com/iZxSi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iZxSi.jpg" alt="database example"></a></p>

<p>I'm using a mysql query to get the data:</p>

<pre><code>$db = new mysqli('localhost', 'username', 'password', 'database');

$postQuery = $db-&gt;query("
        SELECT 
        posts.id, 
        posts.title

        FROM posts

        GROUP BY posts.id
        ");

    while($row = $postQuery-&gt;fetch_object()){
        $posts[] = $row;
    }
</code></pre>

<p>And then I display the data using a foreach loop:</p>

<pre><code>&lt;?php foreach($posts as $post): ?&gt;
&lt;div&gt;&lt;?php echo $post-&gt;title; ?&gt;
etc.
&lt;/div&gt;
 &lt;?php endforeach; ?&gt;
</code></pre>

<p>Everything seems to work, except when I try to use the posts.id in a mysql query to check the content of the table in the databse with the corresponding posts.id. Is there a way to retrieve the id to use it in a query or php function?</p>