从JSON

时间:2017-02-21 19:46:46

标签: php

我得到了这个回复给我:

{
"GROUP_CONCAT(DISTINCT(CONCAT(\"MAX(IF(pa.printer = \"\"\", printer, \"\"\", pa.printer, NULL)) AS \", printer)))":
 "MAX(IF(pa.printer = \"JP4-3\", pa.printer, NULL)) AS JP4-3,MAX(IF(pa.printer = \"JP4-1\", pa.printer, NULL)) AS JP4-1,MAX(IF(pa.printer = \"JP4-2\", pa.printer, NULL)) AS JP4-2,MAX(IF(pa.printer = \"B3\", pa.printer, NULL)) AS B3,MAX(IF(pa.printer = \"A2\", pa.printer, NULL)) AS A2"
    }

我想得到这个的值,但只是字符串。我无法摆脱{}。这应该是如此简单,但无法在任何地方找到答案。

以下只是我要做的事情

主要目标是MySQL数据透视表。接听2个电话,但我很好。

$listQuery = PrintJob::select(DB::raw('GROUP_CONCAT(DISTINCT(CONCAT("MAX(IF(pa.printer = """, printer, """, pa.printer, NULL)) AS ", printer)))'))->get();

这将返回上面的结果:

    $json = $listQuery[0];

然后在此查询中使用它

    $data = PrintJob::select(DB::raw('DATE(calendar.datefield) as date'), DB::raw('SUM(print_jobs.quantity) as sum'), DB::raw($listQuery))
                ->leftJoin('mo_numbers', 'mo_numbers.mo_id', '=', 'print_jobs.mo_id')
                ->rightJoin('calendar', 'calendar.datefield', '=', 'print_jobs.job_date')
                ->whereDate('calendar.datefield', '>=', date($from))
                ->whereDate('calendar.datefield', '<=', date($to))
                ->groupBy('date')
                ->toSql();
                //->get();

1 个答案:

答案 0 :(得分:1)

这是一些奇怪的东西,但是这将获得价值:

$value = current(json_decode($json, true));

获取密钥:

$key = key(json_decode($json, true));

或者一举一动:

list($key, $val) = each(json_decode($json, true));