php echo json in foreach loop

前端 未结 1 1301
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 15:26

I have product ids like this 1,2,3 in $product_ids

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

foreach          


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 16:03

        $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);
    

    0 讨论(0)
提交回复
热议问题