How to return only items that occur in 2 sql select statemnts
<p>I have two different sql statements. <code>$sql</code> grabs all the items whose title matches a certain search text. <code>$cat_sql</code> grabs all the <code>category_items</code> that are in a certain category. An item has an ID. A <code>category_item</code> has a field called <code>item_id</code> which is a foreign key to IDs in the <code>items</code> table</p>

<pre><code>...
mysqli setup code
...

$title = $_POST["title"];
$cat_id = $_POST["cat_id"];

$cat_sql = "SELECT * FROM category_items WHERE category_id = '".$cat_id."'";

$sql = "SELECT * FROM items where title LIKE '%". $title ."%' Limit 70";

if (!$result_cat = $mysqli-&gt;query($cat_sql)) {
// The query failed. 
    echo "&lt;h2 &gt;ERROR&lt;/h2&gt;";
    exit;
}

if (!$result = $mysqli-&gt;query($sql)) {
// The query failed. 
    echo "&lt;h2 &gt;ERROR&lt;/h2&gt;";
    exit;
}
</code></pre>

<p>Then I display all items:</p>

<pre><code>while ($item = $result-&gt;fetch_assoc()) {
    include 'item_card.php';
}
</code></pre>

<p>Currently this just displays all items fetched in the <code>$sql</code> query. Is there some way to remove all items from <code>$result</code> that do not have their ID represented as an <code>item_id</code> in <code>$result_cat</code>?</p>

<p><strong>NOTE:</strong>
I would strongly prefer not to do just combine both SELECT statements into a table join because the actual $sql and $cat_sql are not nearly as simple as I have represented here. Also, they vary depending on which if statement they are in.</p>

<p>My question is: given <code>$result</code> and <code>$result_cat</code>, can I remove items from $result?</p>

<p><strong>EDIT 1</strong>
As suggested by comments I am making an array if item_ids then doing an in_array query. Progress thus far:</p>

<pre><code>$result_cat_ids = [];
while ($cat_item = $result_cat-&gt;fetch_assoc()) {
    $result_cat_ids[] = $cat_item['item_id'];
}
</code></pre>

<p><strong>EDIT 2</strong> Here is the working code following the suggestions in the comments</p>

<pre><code>  if (in_array($item['id'], $result_cat_ids)) {
      include 'item_card.php';
  }
</code></pre>