2

I have product ids like this 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);
}

how can I format json with this code in for loop echo json not working is there any other way plz help

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 に答える