简体   繁体   English

在PHP中加载一组文字数据的最简单方法是什么?

[英]What's the easiest way to load a set of literal data in PHP?

I am building a small gallery that will only have 20 or so images in it. 我正在建立一个小画廊,里面只有20张左右的图像。 I'd like to have this data stored somewhere (even in the PHP file itself) and so am looking at ways of encoding the data as a literal or resource file. 我想将此数据存储在某个位置(甚至在PHP文件本身中),因此我正在寻找将数据编码为文字或资源文件的方法。

In JavaScript, I would use notation resembling (from memory): 在JavaScript中,我将使用类似于(从内存中)的表示法:

var albums = [
  { name='Album 1', photos=['photo1.jpg', 'photo2.jpg'] },
  { name='Album 2', photos=['photo3.jpg', 'photo4.jpg'] }
]

This is JSON, in essence. 本质上,这是JSON。

I suppose JavaScript is more dynamic than PHP and so this is not possible. 我认为JavaScript比PHP更动态,因此这是不可能的。 Is there a simple alternative, perhaps using XML and binding to some classes? 有没有简单的选择,也许使用XML并绑定到某些类?

I'm a complete PHP novice, so please don't assume any knowledge in your answer. 我是PHP的新手,所以请不要在您的答案中假设任何知识。

If you just have some constant data that you want to store in a PHP file, you can use the same thing as you did in Javascript : just declare a PHP array. 如果只有一些常量数据要存储在PHP文件中,则可以使用与Javascript相同的方法:只需声明一个PHP数组。


Something like this should do the trick : 这样的事情应该可以解决问题:

$albums = array(
    array(
        'name' => "Album 1", 
        'photos' => array('photo1.jpg', 'photo2.jpg'), 
    ), 
    array(
        'name' => "Album 2", 
        'photos' => array('photo3.jpg', 'photo4.jpg'), 
    ), 
);

And, then, you just can work with the $albums array. 然后,您可以使用$albums数组。


Of course, it's not easy to update, you have to write some valid PHP code -- but if you only have a couple of images, like you said, it should not be that hard to manage. 当然,更新并非易事 ,您必须编写一些有效的PHP代码-但是,如果您只有几个映像(如您所说的),则应该不难管理。


Another solution would be to store the data in an external file (in XML or JSON, for instance) , and use something like simplexml_load_file or json_decode to read it. 另一种解决方案是将数据存储在外部文件中(例如以XML或JSON格式) ,并使用诸如simplexml_load_filejson_decode的内容读取数据。

But it means a bit more work -- so maybe not that necessary in your case ? 但这意味着需要做更多的工作-也许您的情况没有必要吗?

Save the data in a separate json file, and use PHP's json_decode() function to parse it into PHP objects. 将数据保存在单独的json文件中,然后使用PHP的json_decode()函数将其解析为PHP对象。

Using this approach, you've also got easy access to the data in JavaScript via AJAX, if you ever decide to move in that direction. 使用这种方法,如果您决定朝那个方向移动,还可以通过AJAX轻松访问JavaScript中的数据。

This way is easier to work with than storing data in your PHP file, and it also means that you can trivially update the data programmatically if the need ever arises. 与将数据存储在PHP文件中相比,使用这种方法更容易,并且这意味着如果需要,您可以通过编程方式轻松地更新数据。

In PHP you would probably define this as an array, ie. 在PHP中,您可能会将其定义为数组,即。

$albums = array(
    0 => array('name' => 'Album 1', 'photos' => array('photo1.jpg', 'photo2.jpg')),
    1 => array('name' => 'Album 2', 'photos' => array('photo3.jpg', 'photo4.jpg'))
);

You can do the exact same in PHP: 您可以在PHP中执行完全相同的操作:

$albums = array(
    array(
        'name'   => 'Album 1',
        'photos' => array(
            'photo1.jpg',
            'photo2.jpg',
        )
    ),
    array(
        'name'   => 'Album 2',
        'photos' => array(
            'photo3.jpg',
            'photo4.jpg',
        )
    )
);

But this is bad style. 但这是不好的风格。 You might want to create a class: 您可能要创建一个类:

class Album {

    protected $name;

    protected $files;

    public function __construct($name) {
        $this->name = $name;
    }

    public function addFile($file) {
        $this->files[] = $file;
    }

    public function getFiles() {
        return $this->files;
    }

}

$album1 = new Album('Album1');
$album1->addFile('photo1.jpg');
$album1->addFile('photo2.jpg');

$album2 = new Album('Album2');
$album2->addFile('photo3.jpg');
$album2->addFile('photo4.jpg');

$albums = array($album1, $album2);

var_exportjson_encode是可能的。

file_put_contents($path_to_file, json_encode($albums));

there are number of possibilities 有很多可能性

Don't forget about serialize 不要忘记序列化

serialize — Generates a storable representation of a value 序列化-生成值的可存储表示

serialize works on PHP objects too which JSON and XML don't 序列化也适用于PHP对象,而JSON和XML不支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM