从文件中读取数组键,而不包括该文件


reading an array key from a file, without including the file

我有一个包含数组$myArray的 php 文件。

<?php 
$myArray = array(
  'key1'=>'value1',
  'key2'=>'value2',
);
?>

我需要从该数组中读取key1的值。有没有办法直接读取key1,而不包括整个文件?现在我正在使用这样的包含。

$includedArray = include('/path/to/myArray.php');

但这给我带来了一个问题,因为即使我将其包含在新名称下 $includedArray ,它仍然识别旧名称$myArray这会导致命名冲突。

为了解决这个问题,我将包含的数组从命名数组 ($myArray) 更改为未命名数组,但我无法更改包含的文件。那么有没有办法:

  • 包括包含命名数组的文件,但让它完全忘记原始名称($myArray),并让它只使用我给它的新名称($includedArray)?

  • 或者有没有办法简单地从数组中读取 1 个密钥而不包括整个文件?

然后将数组复制到另一个变量并取消设置原始数组?

path/to/myNewArray.php

return call_user_func(function() {
  include "/path/to/myArray.php";
  return $myArray;
});
/*
if (isset($myArray)) {
  $tmpMyArray = $myArray; // storing the $myArray if defined
}
$includedArray = $myArray;
unset($myArray);
if (isset($tmpMyArray)) {
  $myArray = $tmpMyArray; // restoring the previous $myArray
}
*/

用法:

$whatEver = include("/path/to/myNewArray.php"); // no interference now

如果您需要共享值,但又不想使用与常见的 php 配置文件共享的全局变量,那么将这些值存储在 xml 或 json 文件中怎么样?


使用 Json,您可以将文件中的"数组"加载到您选择的变量中。

http://www.php.net/manual/en/function.json-decode.php


或者,您可以使用输出缓冲区,但这不适用于您当前的用例。

    function ob_include_to_string($filename)
    {
        ob_start();                                         // Starts the 'output buffer'
        include($filename);                                 // Includes the file
        $return_variable = ob_get_contents();               // Sets an variable to keep the content of the echo'ed out content
        ob_end_clean();                                     // Ends and deletes the 'output buffer'; "cleans it up"
        return $return_variable;                            // Returns the variable with the content
    }

将配置文件更改为:

<?php 
$myArray = array(
  'key1'=>'value1',
  'key2'=>'value2',
);
echo json_encode($myArray);
?>

然后你可以做:

$newArray = json_decode(ob_include_to_string('config.php'));

方法 1

利用function scope

$myArray = arrayInclude("myArray.php");
var_dump($myArray);
function arrayInclude($file)
{
    include $file;
    return $myArray;    
}

输出

array
  'key1' => string 'foo1' (length=4)
  'key2' => string 'foo2' (length=4)

我的阵列.php

$myArray = array (
                'key1' => 'foo1',
                'key2' => 'foo2'
        );

方法 2

使用 function & namespace

答.php

include 'b.php';
include 'c.php';
$myArray = call_user_func ( 'b'myArray' );
var_dump ( $myArray );
$myArray = call_user_func ( 'c'myArray' );
var_dump ( $myArray );

输出

array
  'key1' => string 'foo1' (length=4)
  'key2' => string 'foo2' (length=4)
array
  'key1' => string 'bar1' (length=4)
  'key2' => string 'bar2' (length=4)

乙.php

namespace b;
    function myArray() {
        return array (
                'key1' => 'foo1',
                'key2' => 'foo2'
        );
    }

c.php

namespace c;
function myArray() {
    return array (
            'key1' => 'bar1',
            'key2' => 'bar2' 
    );
}

数组.php

<?php
    class MyArray {
        public static $myArray = array('key1' => value1, 'key2' => value2);
    }
?>

其他.php

<?php
    include('array.php');
    //Access the value as
    MyArray::$myArray['key1'];
?>