2

我有这样的产品ID 1,2,3 in$product_ids

$product_ids = explode(',', $product_ids);
$product_ids = array_filter($product_ids);

foreach ($product_ids as $key => $product_id) {

      $sth = $this->db->prepare("SELECT * FROM products Where id =:id ");
      $sth->execute(array( ':id' => $product_id ));
      $final_data = $sth->fetchAll();

      echo json_encode($final_data);
}

我如何json在for循环回显中使用此代码格式化json不工作有没有其他方法请帮助

4

1 回答 1

4
    $product_ids = explode(',', $product_ids);
    $product_ids = array_filter($product_ids);
    $final_data = array();
    foreach ($product_ids as $key => $product_id) {

          $sth = $this->db->prepare("SELECT * FROM products Where id =:id ");
          $sth->execute(array( ':id' => $product_id ));
          $final_data[$product_id] = $sth->fetchAll();


    }
    echo json_encode($final_data);
于 2012-11-12T11:03:21.677 回答