【问题标题】:Multiple array keys from array数组中的多个数组键
【发布时间】:2015-12-07 23:05:45
【问题描述】:

我有以下键数组:

$keys = $array('one', 'two', 'three');

还有这个值:

$value = 'text';

我想创建一个新数组:

$array['one']['two']['three'] = 'text';

我怎样才能做到这一点?

【问题讨论】:

  • 您希望数组成为您的键?

标签: php arrays


【解决方案1】:
$array = array();
$current =& $array;
$keys = $array('one', 'two', 'three');
$value = 'text';

foreach (array_slice($keys, 0, -1) as $k) {
    $current[$k] = array();
    $current = & $current[$k];
}
$current[$keys[count($keys)-1]] = $value;

使用$current 的引用允许它修改嵌套数组。

【讨论】:

  • 它对我不起作用我有一条错误消息:致命错误:函数名称必须是第 4 行的字符串!行目标 $keys = $array('one', 'two', 'three');
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
相关资源
最近更新 更多