从另一个PHP文件中回显一个数组


Echo an array from another PHP file

我目前陷入了一个我认为简单的解决方案。。。我正在使用PHPFileNavigator,我唯一关注的是如何在单独的文件中回显返回到数组的Title。每次为上传的文件创建/编辑文件时,在向文件添加标题时,都会生成以下文件。

更新

一般来说,我想做的就是从我的目标文件中返回一个Array值,在这种情况下,它将来自"titulo"键,然后将其打印回我的源文件。

目标文件

 <?php
 defined('OK') or die();
 return array(
    'titulo' => 'Annual 2011 Report',
    'usuario' => 'admin'
 );
 ?>

源文件

<?php
  $filepath="where_my_destination_file_sits";
  define('OK', True); $c = include_once($filepath); print_r($c);
?>

当前结果

Array ( [titulo] => Annual 2011 Report [usuario] => admin )

建议结果

Annual 2011 Report

我想知道的是,如何将这个数组回声到另一个PHP页面上的变量中?提前谢谢。

假设您的文件保存在$filepath

<?php
define('OK', True);
$c = include_once($filepath);
print_r($c);

如果知道文件名和文件路径,就可以很容易地将php文件的返回构造捕获到文件中。

这里有一个例子:

$filepath = 'path/to/phpfile.php';
$array = include($filepath); //This will capture the array
var_dump($array);

include和return协同工作的另一个示例:[Source:php.net]

return.php

<?php
$var = 'PHP';
return $var;
?>

noretrun.php

<?php
$var = 'PHP';
?>

testreturns.php

<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>

更新

要只打印数组中的项目,可以使用索引。在您的情况下:

<?php
$filepath="where_my_destination_file_sits";
define('OK', True); $c = include_once($filepath); print_r($c);
echo $c['titulo']; // print only the title
?>

首先我们有一个文件(您想在其中初始化数组)first.php

(你也可以扮演专业人士和玩参数,用任何参数函数传递不同类型或不同数组)

 function first_passing() {
      $yourArray=array('everything you want it's be');
      return $yourArray;
     }

并且在second.php

require 'yourpath/first.php' (or include or include_once or require_once )
//and here just call function
$myArray=first_passing();
//do anything want with $myArray

要在PHP中打印/回显数组,必须使用print_r($array_variable)而不是echo $array