Push Array inside an Array PHP
<p>I have a simple query that looks for the same id inside the <code>$_SESSION['cart_items']</code> keys. This is the output:</p>

<p><a href="https://i.stack.imgur.com/6fOYi.png" rel="nofollow"><img src="https://i.stack.imgur.com/6fOYi.png" alt="enter image description here"></a></p>

<p>and the code:</p>

<pre><code>$statement = $conn-&gt;query("SELECT * FROM product WHERE id IN (".implode(',',array_keys($_SESSION['cart_items'])).")");

$data = array();

while($row = $statement-&gt;fetch()) {
    $data[] = $row;
}
print_r($data);
</code></pre>

<p>This works fine but I want to add a 5th element inside the array. The value will be coming from the value of the associative array <code>$_SESSION['cart_items'][$row['id']]</code> inside the while loop. So far what I did:</p>

<pre><code>while($row = $statement-&gt;fetch()) {
    $data[] = $row;
    if(array_key_exists($row['id'], $_SESSION['cart_items']))
    {
        $another = $_SESSION['cart_items'][$row['id']];
        array_push($data, $another);
    }
}
print_r($data);
</code></pre>

<p>But I get this output:</p>

<p><a href="https://i.stack.imgur.com/yAy13.png" rel="nofollow"><img src="https://i.stack.imgur.com/yAy13.png" alt="enter image description here"></a></p>

<p>As you can see, there is an additional <code>[1]=&gt;23</code> and <code>[3]=&gt;47</code> but that's not what I want to happen. What I want to happen is something like this:</p>

<p><a href="https://i.stack.imgur.com/r1Hw4.png" rel="nofollow"><img src="https://i.stack.imgur.com/r1Hw4.png" alt="enter image description here"></a></p>

<p>I want it to be a part of the array inside an array. Or more like the 5th element. Can I do something like this?</p>