doutangtan6386 2018-02-05 04:22
浏览 169
已采纳

将集合的值转换为laravel中的键

this my mysql query:

$tmp=almacen::select('nombre_empresa','oferta')->join('users','users.id','=','almacen.emp_id')->where('almacen.event_id','5')->get();

this returns several objects like these:

   ...
   App\almacen {#1948
     nombre_empresa: "Aux1",
     oferta: "Serv_1234",
   },
   App\almacen {#1947
     nombre_empresa: "Aux2",
     oferta: "Serv 12345678",
   },
  ...

i need to convert "nombre_empresa" in a key, for example

$tmp['Aux2']
this return:
"Serv 12345678"

Is it possible to do this in Laravel? or should I do it in another way?

  • 写回答

3条回答 默认 最新

  • dongxun1142 2018-02-05 04:26
    关注

    Sure Laravel can handle that, check out the available collections methods. mapWithKeys is probably what you're looking for:

    $mapped = $results->mapWithKeys(function ($item) {
        return [$item['nombre_empresa'] => $item['oferta']];
    });
    

    Edit: mapWithKeys rather than map

    https://laravel.com/docs/5.5/collections#method-mapwithkeys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法