当从 MySQL 数据库动态加载元素时,在 PHP 中连接 2 个 JSON 数组
Posted
技术标签:
【中文标题】当从 MySQL 数据库动态加载元素时,在 PHP 中连接 2 个 JSON 数组【英文标题】:Concatenate 2 JSON arrays in PHP when elements loading dynamically from a MySQL database 【发布时间】:2021-01-27 14:48:08 【问题描述】:我正在使用 Laravel API,我有以下要求。我正在查询数据库并接受一些订单。我正在检查一个contract_id,我正在使用2 个SQL 查询来获取带有合同ID 的订单以及没有合同ID 的订单。
public function totalOrdersTest(Request $request)
$from = new DateTime($request->query->get('from'));
$to = new DateTime($request->query->get('to'));
$manufacturer_id = $request->query->get('manu');
$article_template_id = $request->query->get('article_template_id');
if ($article_template_id == '' || $article_template_id == null)
$results_not_null = Order::selectRaw('count(*) not_null_order_count, DATE_FORMAT(orders.created_at, "%Y-%m-%d") as order_date')
->filterManufacturer($manufacturer_id)
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->whereNotNull('order_items.tariff_id')
->whereBetween('orders.created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
$results_null = Order::selectRaw('count(*) null_order_count, DATE_FORMAT(orders.created_at, "%Y-%m-%d") as order_date')
->filterManufacturer($manufacturer_id)
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->whereNull('order_items.tariff_id')
->whereBetween('orders.created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
else
$result = Order::selectRaw('count(*) order_count, DATE_FORMAT(created_at, "%Y-%m-%d") as order_date')
->filterArticleTemplate($article_template_id)
->whereBetween('created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
// Need to concatenate ´$results_not_null´ & ´$results_null´ and take a one JSON array.
从这段代码中,我得到了这个带有 2 个对象的 JSON 数组。
"results_not_null":["not_null_order_count":1,"order_date":"2020-06-09","not_null_order_count":2,"order_date":"2020-06-19","not_null_order_count":1,"order_date":"2020-06-29","not_null_order_count":5,"order_date":"2020-07-06","not_null_order_count":2,"order_date":"2020-07-07","not_null_order_count":1,"order_date":"2020-07-08","not_null_order_count":1,"order_date":"2020-07-15","not_null_order_count":1,"order_date":"2020-07-17","not_null_order_count":2,"order_date":"2020-07-22","not_null_order_count":1,"order_date":"2020-07-31","not_null_order_count":1,"order_date":"2020-08-01","not_null_order_count":1,"order_date":"2020-08-03","not_null_order_count":1,"order_date":"2020-08-15","not_null_order_count":2,"order_date":"2020-08-27","not_null_order_count":1,"order_date":"2020-09-15","not_null_order_count":1,"order_date":"2020-09-17","not_null_order_count":4,"order_date":"2020-09-18","not_null_order_count":2,"order_date":"2020-09-19","not_null_order_count":1,"order_date":"2020-09-20","not_null_order_count":1,"order_date":"2020-09-21","not_null_order_count":2,"order_date":"2020-09-22","not_null_order_count":5,"order_date":"2020-09-23","not_null_order_count":1,"order_date":"2020-09-24","not_null_order_count":2,"order_date":"2020-09-25","not_null_order_count":1,"order_date":"2020-09-26","not_null_order_count":5,"order_date":"2020-09-27","not_null_order_count":5,"order_date":"2020-09-28","not_null_order_count":5,"order_date":"2020-09-29","not_null_order_count":3,"order_date":"2020-09-30","not_null_order_count":4,"order_date":"2020-10-01","not_null_order_count":3,"order_date":"2020-10-02","not_null_order_count":4,"order_date":"2020-10-04","not_null_order_count":1,"order_date":"2020-10-05","not_null_order_count":3,"order_date":"2020-10-06","not_null_order_count":2,"order_date":"2020-10-07","not_null_order_count":1,"order_date":"2020-10-08"],"results_null":["null_order_count":1,"order_date":"2020-06-09","null_order_count":1,"order_date":"2020-06-15","null_order_count":1,"order_date":"2020-06-18","null_order_count":3,"order_date":"2020-06-19","null_order_count":1,"order_date":"2020-06-23","null_order_count":1,"order_date":"2020-06-25","null_order_count":1,"order_date":"2020-06-29","null_order_count":21,"order_date":"2020-07-06","null_order_count":2,"order_date":"2020-07-07","null_order_count":3,"order_date":"2020-07-08","null_order_count":1,"order_date":"2020-07-15","null_order_count":3,"order_date":"2020-07-17","null_order_count":3,"order_date":"2020-07-19","null_order_count":2,"order_date":"2020-07-22","null_order_count":3,"order_date":"2020-07-24","null_order_count":1,"order_date":"2020-07-31","null_order_count":1,"order_date":"2020-08-01","null_order_count":1,"order_date":"2020-08-03","null_order_count":1,"order_date":"2020-08-04","null_order_count":1,"order_date":"2020-08-15","null_order_count":5,"order_date":"2020-08-27","null_order_count":2,"order_date":"2020-09-02","null_order_count":2,"order_date":"2020-09-13","null_order_count":1,"order_date":"2020-09-15","null_order_count":1,"order_date":"2020-09-17","null_order_count":4,"order_date":"2020-10-01"]
但我需要连接这 2 个 JSON 数组。两个数组中最重要的任务是有相同的日期,然后我需要在一个元素中使用它们。例如,
[
"order_date":"2020-12-12",
"not_null_order_count": 2,
"null_order_count":3
,
"order_date":"2020-12-13",
"not_null_order_count": 12,
"null_order_count":5
,
"order_date":"2020-12-14",
"null_order_count":9
]
这是我需要的输出。有时一个日期只有 null_order_count 或 not_null_order_count。
有没有什么方法可以使用一个 mysql 查询来做到这一点?或者如何将这 2 个数组组合成一个 JSON 数组?请指教。
【问题讨论】:
你能发布最终的 sql 查询吗?您可以使用 SUM(CASE WHEN .. 进行 1 个查询,但我很确定您必须使用本机 sql 编写它 @AngelDeykov 嘿,你是说 Laravel ORM 生成的原生 SQL 查询吗? 是的,你也可以发一下 @AngelDeykov 请在此处查看代码:gist.github.com/chanakaDe/0a4d0e89e0f13dd818e0d3aa8437158b 【参考方案1】:尝试合并两个 json:
\json_encode(array_merge(\json_decode($json1, true), \json_decode($json2, true)))
【讨论】:
它不工作。我需要组合这两个数组,然后有以下响应。 [ “order_date”:“2020-12-12”,“not_null_order_count”:2,“null_order_count”:3,“order_date”:“2020-12-13”,“not_null_order_count”:12,“null_order_count”: 5 , "order_date":"2020-12-14", "null_order_count":9 ]以上是关于当从 MySQL 数据库动态加载元素时,在 PHP 中连接 2 个 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章
PHP 警告:PHP 启动:无法加载动态库 '/usr/lib/php/20151012/mysql.so'