gpt4 book ai didi

php - 在设置它的同一数组内访问变量

转载 作者:行者123 更新时间:2023-12-02 04:34:41 24 4
gpt4 key购买 nike

我想访问同一个数组中的一个变量,它被设置为执行字符串操作和类似的东西。

我有一个数组,其中包含有关特定目录路径的信息。现在我想执行 str_replace()$options['path_absolute'] 中删除 $options['path_htdocs'] 以获取路径我想要,但 PHP 说它不知道该变量:** 注意: undefined variable :第 12 行的 path\to\files\index.php 中的选项**

// shortened version of that array
$options = [
'path_absolute' => formatPath( dirname(__FILE__) ),
'path_htdocs' => formatPath( $_SERVER["DOCUMENT_ROOT"] ),
'path_from_docroot' => ?
];

这是可能的、不可能的还是完全胡说八道?

最佳答案

因为你的数组没有定义,你不能在定义它的时候引用它,这意味着你必须用两种方法之一来做:

$options = [
'path_absolute' => formatPath( dirname(__FILE__) ),
'path_htdocs' => formatPath( $_SERVER["DOCUMENT_ROOT"] ),
'path_from_docroot' => str_replace(formatPath( $_SERVER["DOCUMENT_ROOT"] ), '', formatPath( dirname(__FILE__) ))
];

$options = [
'path_absolute' => formatPath( dirname(__FILE__) ),
'path_htdocs' => formatPath( $_SERVER["DOCUMENT_ROOT"] ),
];

$options['path_from_docroot'] = str_replace($options['path_htdocs'], '', $options['path_absolute']);

关于php - 在设置它的同一数组内访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22223154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com