gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:31:22 25 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/

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