Php 在同一数组中设置Access变量
我想访问同一数组中的一个变量,它被设置为执行字符串操作之类的操作 我有一个数组,其中包含指向特定目录的路径信息。现在我想做Php 在同一数组中设置Access变量,php,arrays,Php,Arrays,我想访问同一数组中的一个变量,它被设置为执行字符串操作之类的操作 我有一个数组,其中包含指向特定目录的路径信息。现在我想做str_replace()从$options['path\u htdocs']中删除$options['path\u absolute']以获得我想要的路径,但PHP说它不知道该变量: **注意:未定义变量:第12行path\to\files\index.php中的选项** // shortened version of that array $options = [ '
str_replace()
从$options['path\u htdocs']
中删除$options['path\u absolute']
以获得我想要的路径,但PHP说它不知道该变量:
**注意:未定义变量:第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__) ))
];
或
我认为您可以使用作用域和permofm来访问数组元素,您的replace分配给
$options['path\u from\u docroot']
您的新字符串
$options['path_from_docroot'] = str_replace($options['path_htdocs'], '', $options['path_absolute']);
如果您需要处理的最后一个索引是来自_docroot的路径,您可以这样处理:
$options = [
// ...
'path_absolute' => formatPath( dirname(__FILE__) ),
'path_htdocs' => formatPath( $_SERVER["DOCUMENT_ROOT"] ),
// ...
];
$options['path_from_docroot'] = str_replace($options['path_htdocs'], '', $options['path_absolute']);
$options = [
// ...
'path_absolute' => formatPath( dirname(__FILE__) ),
'path_htdocs' => formatPath( $_SERVER["DOCUMENT_ROOT"] ),
// ...
];
$options['path_from_docroot'] = str_replace($options['path_htdocs'], '', $options['path_absolute']);