【问题标题】:How do I print multiple json arrays如何打印多个 json 数组
【发布时间】:2020-09-27 02:34:12
【问题描述】:

我正在使用如下数组遍历数据库

$checkedProducts = $request->input('products');
$p = null;

foreach($checkedProducts as $checkedProduct){
    $p .= DB::table('products')->where('id', $checkedProduct)->get();
}
dd($p);

这是输出。

[{"id":"6","category_id":"1","shop_id":"31","name":"df","slug":"fgh","details":"hgfh","description":"gf","image":"\/storage\/RZbE9tqTPL3boEVztOfjR1P7IgFeX0OpMMzr21rO.jpeg","sku":"hjhg","price":"456","minimum_order_quantity":"0","stock":"67","out_of_stock":"56","featured":"1","created_at":"2020-06-01 11:15:14","updated_at":"2020-06-01 11:15:14"}][{"id":"7","category_id":"1","shop_id":"31","name":"erty","slug":"bvn","details":"fghj","description":"ghj","image":"\/storage\/J4w9kf06vkCRx4c6S7BvBHV2TA1tYrZuv1sUG5KQ.jpeg","sku":"hj","price":"23","minimum_order_quantity":"0","stock":"45","out_of_stock":"45","featured":"1","created_at":"2020-06-01 11:15:32","updated_at":"2020-06-01 11:15:32"}][{"id":"8","category_id":"1","shop_id":"31","name":"werty","slug":"thyju","details":"red","description":"ytyu","image":"\/storage\/Wj09GjAkmlbk2GZHwVeaJLRLkJE83gryYAsUdvna.jpeg","sku":"dwg","price":"2345","minimum_order_quantity":"0","stock":"5000","out_of_stock":"50","featured":"1","created_at":"2020-06-02 08:32:06","updated_at":"2020-06-02 08:32:06"}]

如何在刀片文件中打印出来?我已经尝试过 forEach 循环,但它不起作用

【问题讨论】:

  • 请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于SO的相关主题;如果您遇到困难,请发布您的尝试minimal reproducible example,并使用[<>] sn-p 编辑器记录输入和预期输出。
  • 你的 JSON 不是一个有效的数组,而是几个数组——你需要服务器生成有效的 JSON 或者你需要拆分

标签: html arrays json laravel


【解决方案1】:
  1. 使 JSON 在源上有效

检查 json documentation 这是有效 JSON 的简单规则

在您的情况下,您有 array1array2array3 将它们放在另一个数组中 [array1,array2,array3] 逗号分隔

  1. 您可以轻松打印出来

const arrays = [
[{"id":"6","category_id":"1","shop_id":"31","name":"df","slug":"fgh","details":"hgfh","description":"gf","image":"\/storage\/RZbE9tqTPL3boEVztOfjR1P7IgFeX0OpMMzr21rO.jpeg","sku":"hjhg","price":"456","minimum_order_quantity":"0","stock":"67","out_of_stock":"56","featured":"1","created_at":"2020-06-01 11:15:14","updated_at":"2020-06-01 11:15:14"}],
[{"id":"7","category_id":"1","shop_id":"31","name":"erty","slug":"bvn","details":"fghj","description":"ghj","image":"\/storage\/J4w9kf06vkCRx4c6S7BvBHV2TA1tYrZuv1sUG5KQ.jpeg","sku":"hj","price":"23","minimum_order_quantity":"0","stock":"45","out_of_stock":"45","featured":"1","created_at":"2020-06-01 11:15:32","updated_at":"2020-06-01 11:15:32"}],
[{"id":"8","category_id":"1","shop_id":"31","name":"werty","slug":"thyju","details":"red","description":"ytyu","image":"\/storage\/Wj09GjAkmlbk2GZHwVeaJLRLkJE83gryYAsUdvna.jpeg","sku":"dwg","price":"2345","minimum_order_quantity":"0","stock":"5000","out_of_stock":"50","featured":"1","created_at":"2020-06-02 08:32:06","updated_at":"2020-06-02 08:32:06"}]
];

arrays.forEach(item => console.log(item));

【讨论】:

    【解决方案2】:

    您的 JSON 无效。您需要对其进行操作以将数据放入适当的对象中:

    const str = `[{"id":"6","category_id":"1","shop_id":"31","name":"df","slug":"fgh","details":"hgfh","description":"gf","image":"\/storage\/RZbE9tqTPL3boEVztOfjR1P7IgFeX0OpMMzr21rO.jpeg","sku":"hjhg","price":"456","minimum_order_quantity":"0","stock":"67","out_of_stock":"56","featured":"1","created_at":"2020-06-01 11:15:14","updated_at":"2020-06-01 11:15:14"}][{"id":"7","category_id":"1","shop_id":"31","name":"erty","slug":"bvn","details":"fghj","description":"ghj","image":"\/storage\/J4w9kf06vkCRx4c6S7BvBHV2TA1tYrZuv1sUG5KQ.jpeg","sku":"hj","price":"23","minimum_order_quantity":"0","stock":"45","out_of_stock":"45","featured":"1","created_at":"2020-06-01 11:15:32","updated_at":"2020-06-01 11:15:32"}][{"id":"8","category_id":"1","shop_id":"31","name":"werty","slug":"thyju","details":"red","description":"ytyu","image":"\/storage\/Wj09GjAkmlbk2GZHwVeaJLRLkJE83gryYAsUdvna.jpeg","sku":"dwg","price":"2345","minimum_order_quantity":"0","stock":"5000","out_of_stock":"50","featured":"1","created_at":"2020-06-02 08:32:06","updated_at":"2020-06-02 08:32:06"}]`
    
    const dl = document.getElementById("dl");
    
    let dlContent = [];
    JSON.parse("[" + str.split("][").join("],[") + "]").forEach((arr, i) => {
      dlContent.push(`<dt><h3>item ${i}</h3></dd>`);
      Object.entries(arr[0]).forEach(([k, v]) => dlContent.push(`<dt>${k}</dt><dd>${v}</dd>`))
    })
    dl.innerHTML = dlContent.join("")
    dt {
      float: left;
      clear: left;
      width: 10em;
      font-weight: bold;
    }
    
    dd {
      float: left;
      word-break: break-all;
    }
    &lt;dl id="dl"&gt;&lt;/dl&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2019-08-26
      • 2015-05-08
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多