【问题标题】:how to get two columns value in laravel where first is key and 2nd is value?如何在laravel中获取两列值,其中第一个是键,第二个是值?
【发布时间】:2021-04-06 23:08:41
【问题描述】:

我有 2 列 id = [10 , 22 , 31];loction_url = [123.456 , 654.325 , 632,983]; 我希望数据像这样$a = [10 => 123.456 , 22 => 654.325 , 31 => 632,983];

这是我的查询,它只获取列

$customers = Customer::whereIn('created_by', $adminot)->select(array('id' , 'location_url'))->get();

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

这就是Collection::pluck 的用途:

$customers = Customer::whereIn('created_by', $adminot)
    ->select(array('id' , 'location_url'))
    ->get()
    ->pluck('location_url', 'id');

它会返回一个关联数组,其中键是id 的值,其各自的值是location_url 的值。

【讨论】:

    【解决方案2】:

    你可以像使用 array_merge 一样

    $a = array_merge($id,$loction_url)
    

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 2021-10-16
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2018-11-05
      相关资源
      最近更新 更多