如何打印多个 json 数组

Posted

技术标签:

【中文标题】如何打印多个 json 数组【英文标题】:How do I print multiple json arrays 【发布时间】: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 或者你需要拆分 【参考方案1】:
    使 JSON 在源上有效

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

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

    您可以轻松打印出来

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;

【讨论】:

以上是关于如何打印多个 json 数组的主要内容,如果未能解决你的问题,请参考以下文章

如何在php中打印json文件数组中的数据

如何在laravel中使用json将json数组打印到jquery表?

如何在 PHP 中获取 JSON 数组并打印其值?

如何从嵌套 Json 数组角度 2 打印和分离键和值并将其添加到选择框中

如何使用数组打印多个枚举值?

PowerShell - 使用排序的对象数组打印 JSON 输出?