删除每次迭代后添加的extra []以通过PHP发送JSON

public function  showcarts($email){
    $sql = 'SELECT * FROM users WHERE email = :email';
    $query0 = $this -> conn -> prepare($sql);
    $query0 -> execute(array(':email' => $email));
    $data = $query0 -> fetchObject();
    $p_cart= $data -> p_cart;  
    $p_cart = preg_replace('/\.$/', '', $p_cart); //Remove dot at end if exists
    $array = explode(',', $p_cart); //split string into array seperated by ', '
    $projects=array();
    foreach($array as $p_id) //loop over values
    {   
        $sql = 'SELECT * FROM products WHERE p_id = :p_id';
        $query = $this -> conn -> prepare($sql);
        $query -> execute(array(':p_id' => $p_id)); 
        if($query){
            while($products = $query -> fetchAll(PDO::FETCH_OBJ))
            {
                 array_push($projects,$products);
            }
        }         
    }
    return $projects;   
}

enter image description here

This function is returning the objects as required but adding [] after every iteration. I want to remove the extra [] so that it can fit to my modal object. Is there a way to do this ?

fetchAll fetches all rows immediately, so it's redundant to use while:

if($query){
    $projects = $query -> fetchAll(PDO::FETCH_OBJ);
}