php - 将 explode 数组作为键添加到新数组

标签 php arrays explode

$input = "hello|world|look|at|this";
$explode = explode("|", $input);
$array = array("Title" => "Hello!", "content" => $explode);

这将输出:

array(2) {
  ["Title"]=>
  string(6) "Hello!"
  ["content"]=>
  array(5) {
    [0]=>
    string(5) "hello"
    [1]=>
    string(5) "world"
    [2]=>
    string(4) "look"
    [3]=>
    string(2) "at"
    [4]=>
    string(4) "this"
  }
}

但我希望它们是带有 NULL 作为值的键,因为我会在后面的步骤中添加值。

知道如何让 explode() 函数作为键返回吗? 有函数吗可用吗?

最佳答案

array_fill_keys 可以基于数组填充键:

array_fill_keys ($explode, null);

关于php - 将 explode 数组作为键添加到新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21133333/

相关文章:

尝试迁移时出现 PHP Fatal error out of memory 错误

php - 我正在尝试拆分/分解/preg_split 一个字符串,但我想保留分隔符

PHP,试图将 $_POST ['var' ] 作为文本放入表中

c - 重新分配数组索引

c - 如何从用户那里获取输入作为 C 中的整数数组?

PHP:如何将变量放入数组中?

php - 使用 PHP,从 MySQL 字段检索数组并用它来填充复选框组

PHP从具有范围的字符串中获取数字数组

php - UrlGenerationException(缺少 [Route] 所需的参数)

Phpunit,如何删除模拟对象?