【问题标题】:reading an array key from a file, without including the file从文件中读取数组键,不包括文件
【发布时间】:2012-05-17 06:57:43
【问题描述】:

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

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

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

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

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

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

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

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

【问题讨论】:

  • 您是否考虑过在沙箱中运行包含? php.net/manual/en/runkit.sandbox.php
  • include 没有返回值,那么这怎么工作?
  • @msgmash.com 我包含的文件有一个命名数组,最后有一个返回。我无法对该文件进行任何更改。只能包含它。
  • @vedarthk 以前从未在 php 中使用过命名空间。你能写一个答案吗?
  • 没有不涉及更改原始文件的简单解决方案。

标签: php arrays include


【解决方案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

【讨论】:

  • 嗯,我有类似的事情正在发生。我希望我可以将新数组称为干净,而不必这样做。
  • 您可以将该代码块封装到一个新的包含文件中,当您返回结果时:return $includedArray,然后像这样使用它:$myArrayWithFreeName = include "/path/to/myEncapsulatedArray.php";
  • 将其封装在一个函数中,直接调用。
【解决方案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'));

【讨论】:

  • 这是错误的......ob_get_contents() 只会在$filename 输出带有 print 或 echo 或类似内容的内容时返回值......它不会将数组作为变量返回
  • 对不起,我在想什么……在这个用例中不太合乎逻辑。
【解决方案3】:

方法一

利用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

a.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' 
    );

}

【讨论】:

  • 我认为是的,将其声明为类的静态变量将是实现目标的有效方法。
【解决方案4】:

array.php

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

其他.php

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

    //Access the value as
    MyArray::$myArray['key1'];
?>

【讨论】:

    猜你喜欢
    • 2016-12-29
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多