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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题