PHP的回声JSON在foreach循环 [英] php echo json in foreach loop

查看:115
本文介绍了PHP的回声JSON在foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在$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);
}

我如何使用for循环回显json中的这段代码格式化json,否则请问有什么其他方法

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

推荐答案

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

这篇关于PHP的回声JSON在foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆