php - 如何替换爆炸中的数组键

标签 php laravel

我有如下代码

$string = "Trainee,Beginner";

我想用 explode 替换 $string 到数组对象

$list = explode(',', $string);

我得到的结果。

array:2 [▼
  0 => "Trainee"
  1 => "Beginner"
];

我想要的结果。

array:2 [▼
  'Trainee' => "Trainee"
  'Beginner' => "Beginner"
];

最佳答案

您可以使用 array_combine() 将一个数组作为键,将另一个数组作为值。所以只需为两个参数传递 $list 就可以了。

<?php
$string = "Trainee,Beginner";
$list = explode(',', $string);
$final_array = array_combine($list, $list);
print_r($final_array);
?>

演示: https://3v4l.org/vmgaH

关于php - 如何替换爆炸中的数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55804186/

相关文章:

laravel - 无法在 Gitlens 中看到文件历史记录(在特定项目上)

php - 在login_check中返回Json

php - 使用 XPath 和 PHP 解析 HTML

php - ISSET 与 JQUERY 的执行顺序

php - Lumen中的隐式路由模型绑定(bind)?

php - Laravel 4 ViewModel(带 mustache )

laravel - 从另一个内部的 Controller 调用方法

php - 我怎样才能让我的 curl 在抓取之前等待几秒钟?

php - 如何使用php根据mysql表中的数据自动创建div?

php - 为什么我在 Laravel 的 Eloquent ORM 中获取帐户 ID 作为用户 ID