dtvgo28624 2017-02-21 19:46
浏览 256
已采纳

从JSON中获取对象属性值

I am getting this returned to me:

{
"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"
    }

I want to get the value of this but just the string. I can't get it out of the {}. This should be so simple but cant find an answer anywhere.

BELOW IS JUST REFERENCE TO WHAT I AM DOING

The main goal is a MySQL pivot table. Takes 2 calls but I am OK with that.

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

This is returning the result above:

    $json = $listQuery[0];

Then going to use it in this query

    $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条回答 默认 最新

  • dongyi5817 2017-02-21 19:52
    关注

    That's some weird stuff to get, however this will get the value:

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

    To get the key:

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

    Or in one swoop:

    list($key, $val) = each(json_decode($json, true));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含