【发布时间】: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 或者你需要拆分