如何在php文件中返回数组
我的一个php文件中有以下数组,我想通过另一个文件返回该数组 set-info.php如何在php文件中返回数组,php,arrays,arr,Php,Arrays,Arr,我的一个php文件中有以下数组,我想通过另一个文件返回该数组 set-info.php <?php return [ 'email' => [ 'mail-to@mail.com', 'mail-from@mail.com', ] 我得到的php标签作为输出完整的内容。 如何仅获取php数组?请指教 ]; 尝试使用: public function getInfo() { //read file contents from
<?php
return [
'email' => [
'mail-to@mail.com',
'mail-from@mail.com',
]
我得到的php标签作为输出完整的内容。
如何仅获取php数组?请指教
]; 尝试使用:
public function getInfo()
{
//read file contents from file
$filename =$_SERVER['DOCUMENT_ROOT'].'/set-info.php';
$filename = str_replace(" ", "", $filename);
if (file_exists($filename)) {
$str = (include $filename);
return $str;
}
}
您需要使用
include
for来获取可以在PHP中使用的数组。使用file\u get\u contents
可以将文件内容作为字符串。
按以下方式更改您的方法:
public function getInfo()
{
$info = include 'set-info.php';
//Now $info will be the array from set-info.php file.
}
您可以在此处查看更多示例:文件获取内容可能希望成为
包含
或要求
。从函数而不是从文件中返回它<代码>函数arraymaker(){$myarray=['apple','orange'];返回$myarray;}$arr=include“set info.php”代码>@accountaryم一个文件实际上可以返回一个值。@franzgleichman哇!,我不知道,谢谢我会照顾你的
public function getInfo()
{
$info = include 'set-info.php';
//Now $info will be the array from set-info.php file.
}