数组索引是否可以从同一数组

时间:2018-05-13 13:40:37

标签: php arrays

问题是"数组索引可以从同一数组中的其他数组索引获取值吗?"比如当你有一个像这样的数组变量时:

$conf = [
  "app" => [
    "name" => "myApplicationName",
    "key" => md5($conf['app']['name']) // I mean to call itsself
    // I tried using $this but return into error

  ]
]

当我写$conf['app']['name']时,它会返回错误:

  

未定义的变量:conf in ..

那么我可以从同一个数组中的其他数组索引调用或获取数组索引的值吗?

1 个答案:

答案 0 :(得分:3)

正如您所见,由于PHP解析数组的定义方式,该语法不起作用。您可以单独定义它们:

$conf["app"]["name"] = "myApplicationName";
$conf["app"]["key"]  = md5($conf['app']['name']);

或者定义大数组并添加它:

$conf = [
  "app" => [
    "name" => "myApplicationName",
    "something" => "something"
    //add more keys etc...
  ]
];
$conf["app"]["key"] = md5($conf['app']['name']);