1

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

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

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

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

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

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

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

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

4

4 回答 4

3

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

路径/到/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
于 2012-05-08T19:01:18.077 回答
2

如果您需要共享值,但又不想使用与公共包含的 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'));
于 2012-05-08T19:09:23.327 回答
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)

myArray.php

$myArray = array (

                'key1' => 'foo1',
                'key2' => 'foo2'
        );

方法二

使用 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)

b.php

namespace b;

    function myArray() {
        return array (

                'key1' => 'foo1',
                'key2' => 'foo2'
        );

    }

c.php

namespace c;

function myArray() {
    return array (

            'key1' => 'bar1',
            'key2' => 'bar2' 
    );

}
于 2012-05-08T19:04:51.677 回答
0

数组.php

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

其他.php

<?php
    include('array.php');

    //Access the value as
    MyArray::$myArray['key1'];
?>
于 2012-05-08T19:34:54.990 回答