如何使用编码器从php文件加载数组


How to load array from php file using codeigniter?

我有很多数据在数组中,但我没有插入到数据库中,是否可以将它们保存在php文件中,并在使用时加载?

的例子:

$myArray = fileContent;

我的文件myFile.php

array(
    array( [ 'id' ] => 12, [ 'nome' ] => "Acre", [ 'sigla' ] => "AC" ),
    array( [ 'id' ] => 27, [ 'nome' ] => "Alagoas", [ 'sigla' ] => "AL" ),
    array( [ 'id' ] => 16, [ 'nome' ] => "Amapa", [ 'sigla' ] => "AP" ),
    array( [ 'id' ] => 13, [ 'nome' ] => "Amazonas", [ 'sigla' ] => "AM" ),
    array( [ 'id' ] => 29, [ 'nome' ] => "Bahia", [ 'sigla' ] => "BA" ),
    // and more...
);

另一种方法是创建一个这样的文件(命名为"data.php"):

<?php
return array(
    array( [ 'id' ] => 12, [ 'nome' ] => "Acre", [ 'sigla' ] => "AC" ),
    array( [ 'id' ] => 27, [ 'nome' ] => "Alagoas", [ 'sigla' ] => "AL" ),
    array( [ 'id' ] => 16, [ 'nome' ] => "Amapa", [ 'sigla' ] => "AP" ),
    array( [ 'id' ] => 13, [ 'nome' ] => "Amazonas", [ 'sigla' ] => "AM" ),
    array( [ 'id' ] => 29, [ 'nome' ] => "Bahia", [ 'sigla' ] => "BA" ),
    // and more...
);
然后加载
$data = require("/path/to/data.php");

如果是静态内容,可以直接在控制器动作中加载。

public function index()
{
    $data['title'] = 'Page Title';
    $data['metaDesc'] = 'meta tags';
    $data['metaKeywords'] = '';                         
    $this->load->view('articles/view', $data);
}
<?php echo $title; ?>