arrays - 如何在 Laravel 中加入数组和 MySQL 表
问题描述
我有一个像
array:5 [▼
188 => array:17 [▼
"user_id" => "176"
"product_id" => "188"
"qty" => "2"
"date" => "03-05-2020"
"product_type" => "rear type"
"custom_color_title" => ""
"custom_color_price" => ""
"bolt_title" => ""
"bolt_price" => ""
"hub_center_rings_title" => ""
"hub_center_rings_price" => ""
"wheel_spacers_title" => ""
"wheel_spacers_price" => ""
"tire_pressure_title" => ""
"tire_pressure_price" => ""
"product_price" => 1890
"product_size" => ""
]
176 => array:17 [▼
"user_id" => ""
"product_id" => "176"
"qty" => "2"
"date" => "03-05-2020"
"product_type" => "wheel type"
"custom_color_title" => ""
"custom_color_price" => ""
"bolt_title" => ""
"bolt_price" => ""
"hub_center_rings_title" => ""
"hub_center_rings_price" => ""
"wheel_spacers_title" => ""
"wheel_spacers_price" => ""
"tire_pressure_title" => ""
"tire_pressure_price" => ""
"product_price" => 1680
"product_size" => ""
]
224 => array:17 [▶]
]
从会话变量这个数组和 mysql 表字段是 id、name、img 等。如何加入 array.product_id 和 table.id ,
我的查询就像$table=DB::select('SELECT * FROM products');
我在 laravel 中所做的那样以任何方式加入 mysql 表和数组?
解决方案
您要加入的 mysql 转储丢失。但我想我明白你想要:
foreach($sessionArray as $sessionArrayKey => $sessionArrayVal)
{
$findInDb = array_search($sessionArrayVal['product_id'], array_column($dbArray, 'id'));
if ($findInDb)
{
$sessionArray[$sessionArrayKey] = array_merge($sessionArray[$sessionArrayKey],$dbArray[$findInDb])
}
}
推荐阅读
- python - 我可以更改 Pandas 中均值函数的 dtype 吗?我想把它改成int类型
- docker - 无法通过 Internet 访问我的本地 kubernetes 服务
- flutter - 仅当热重新加载页面时,设置状态才不会更改值
- swift - 无法格式化 UITableView 的部分标题
- php - Symfony 5,迁移到数据库 mariadb 的教义问题
- javascript - 如何在 ECMAScript 类中调用方法
- python - 使用 zeep 对无限的任务列表进行异步错误处理和响应处理
- node.js - 使用在另一个文件中定义的 Mongoose 模型返回 Undefined
- artificial-intelligence - 谁有权访问 OpenAi API GPT-3?
- scala - Play-JSON 找不到类型参数 T 的隐式 Writes[T]