Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在同一数组中设置Access变量_Php_Arrays - Fatal编程技术网

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']);