首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将集合转换为键值集合

将集合转换为键值集合
EN

Stack Overflow用户
提问于 2022-06-24 05:44:07
回答 1查看 62关注 0票数 1

我有以下收藏:

代码语言:javascript
复制
$configuration = DB::table('configuration')->get();

Illuminate\Support\Collection {#562 ▼
  #items: array:7 [▼
    0 => {#1447 ▼
      +"configuration_name": "assign_device_email_addresses"
      +"configuration_value": "mobiles@example.com|accountspayable@example.com"
    }
    1 => {#1357 ▼
      +"configuration_name": "helpdesk_platform_url"
      +"configuration_value": "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
    }
    2 => {#1446 ▼
      +"configuration_name": "mail_encryption"
      +"configuration_value": "tls"
    }
    3 => {#563 ▼
      +"configuration_name": "mail_host"
      +"configuration_value": "exampleserver.example.com"
    }
    4 => {#1292 ▼
      +"configuration_name": "mail_password"
      +"configuration_value": "encrypted_password"
    }
    5 => {#1291 ▼
      +"configuration_name": "mail_port"
      +"configuration_value": "465"
    }
    6 => {#885 ▼
      +"configuration_name": "mail_username"
      +"configuration_value": "mobiles"
    }
  ]
}

使用这种结构,我只能通过以下语句访问每个项目:

代码语言:javascript
复制
  $configuration[0]->configuration_name
  $configuration[0]->configuration_value
  $configuration[1]->configuration_name
  $configuration[1]->configuration_value
etc.

我希望能够通过以下途径访问:

代码语言:javascript
复制
  $configuration->assign_device_email returning "mobiles@example.com|accountspayable@example.com".
  $configuration->mail_host returning "exampleserver.example.com".
etc.

如何转换这个集合,以便通过它的configuration_name访问每个属性值?

我尝试了下面的方法,这让我更接近了,但是我仍然无法通过诸如$configuration->mail_port等语句访问它。

代码语言:javascript
复制
$configuration2 = DB::table('configuration')->get()
    ->mapWithKeys(function($configuration2){
            return [$configuration2->configuration_name => $configuration2->configuration_value]; 
    });

我认为这是失败的,因为上面的语句仍然返回一个数组:

代码语言:javascript
复制
Illuminate\Support\Collection {#1500 ▼
  #items: array:7 [▼
    "assign_device_email_addresses" => "mobiles@example.com|accountspayable@example.com"
    "helpdesk_platform_url" => "https://clouddesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
    "mail_encryption" => "tls"
    "mail_host" => "exampleserver.example.com"
    "mail_password" => "encrypted_password"
    "mail_port" => "465"
    "mail_username" => "mobiles"
  ]
}

有人有什么想法吗?我觉得我和它越来越近了,但是收藏品让我有点困惑。

我想我本质上是在追求这样的东西:

代码语言:javascript
复制
Illuminate\Support\Collection {#1507 ▼
    +"assign_device_email_addresses" : "mobiles@example.com"
    +"helpdesk_platform_url" : "https://cloudesk.com/app/itdesk/ui/requests/{helpdeskurl}/details"
    +"mail_encryption" : "tls"
    +"mail_host" : "emailhost@example.com"
    +"mail_password" : "encrypted_password"
    +"mail_port" : "465"
    +"mail_username" : "mobiles"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-24 07:54:43

您可以使用Arr助手来映射键值:

代码语言:javascript
复制
$configuration = DB::table('configuration')->get();
$configuration =  Arr::pluck($configuration, 'configuration_value','configuration_name');

此外,添加以下行以导入类:

代码语言:javascript
复制
use Illuminate\Support\Arr;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72739481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档