Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 如何为同一数组的下一个索引分配数组值_Php_Arrays - Fatal编程技术网

Php 如何为同一数组的下一个索引分配数组值

Php 如何为同一数组的下一个索引分配数组值,php,arrays,Php,Arrays,我正在处理包含巨大数组的config文件,而不是再次分配相同的值,我可以重用在创建数组时已分配给其他索引(或键)的值吗?这是我的密码 <?php return [ //array //array //array 'ar' => [ 'mainDirectory' => 'http://example.com/main/', 'subDirectory' => 'http://example.com/main/

我正在处理包含巨大数组的config文件,而不是再次分配相同的值,我可以重用在创建数组时已分配给其他索引(或键)的值吗?这是我的密码

<?php

return [

   //array
   //array
   //array

   'ar' => [
       'mainDirectory' => 'http://example.com/main/',

       'subDirectory'  =>  'http://example.com/main/sub/',

       // instead of using above can't I reuse which is already exist something like below
       // 'subDirectory'  =>  [ar.mainDirectory].'sub/'
    ]

   //array
   //array
   //array
];
?>

我认为我们不能这样做。如果您需要访问一些常用值,那么为什么不尝试使用常量,然后在数组中所有必需的位置使用它呢。

是的,您可以这样做。下面是代码

<?php
    return [
        'ar' => [
            'mainDirectory' => $ref = 'http://example.com/main/',
            'subDirectory'  => $ref.'sub/',
        ]
    ];
?>
检查此问题:
<?php

function arrayRef(){
    return [
        'ar' => [
           'mainDirectory' => $ref = 'http://example.com/main/',
           'subDirectory'  =>  $ref.'sub/',
        ]
    ];
}

print_r(arrayRef());

?>
Array
(
    [ar] => Array
        (
            [mainDirectory] => http://example.com/main/
            [subDirectory] => http://example.com/main/sub/
        )

)