简体   繁体   English

将结构化文本文件内容放入PHP数组

[英]get structured text file content into PHP array

I have a content in a text file, split in blocks which are further split into field/value pairs. 我在文本文件中有一个内容,分为多个块,这些块进一步分成字段/值对。 For example: 例如:

title:: Article 1 Title
----
link:: www.link.to/a_site
----
file:: image1.png
----
description:: Some markdown text content describing the image
====
title:: Article 2 Title
----
link:: www.link.to/another_site
----
file:: image2.png
----
description:: Some more markdown text content describing another image

Using this method ( modified from an earlier question/amswer I found here ): 使用此方法( 根据我在此处找到的先前的问题/答案进行了修改 ):

<?php
$example = array();
$file = explode("====", file_get_contents("content/example.md"));
    foreach ( $file as $content ) {
$example[] = array_filter(array_map("trim", explode("----", $content)));
}

var_dump($example);

?>

I get each block of content into an array, but not the key/value pairs: 我将每个内容块放入一个数组中,但没有键/值对:

array (size=2)
  0 => 
    array (size=4)
      0 => string 'title:: Article 1 Title' (length=23)
      1 => string 'link:: www.link.to/a_site' (length=25)
      2 => string 'file:: image1.png' (length=17)
      3 => string 'description:: Some markdown text content describing the image' (length=61)
  1 => 
    array (size=4)
      0 => string 'title:: Article 2 Title' (length=23)
      1 => string 'link:: www.link.to/another_site' (length=31)
      2 => string 'file:: image2.png' (length=17)
      3 => string 'description:: Some more markdown text content describing another image' (length=70)

The second example given in that answer successfully splits up my key/value pairs: 该答案中给出的第二个示例成功分割了我的键/值对:

<?php
$example = array();
$file = explode("====", file_get_contents("content/example.md"));
foreach ( $file as $content ) {
    foreach(array_filter(array_map("trim",explode("----", $content))) as $line)
    {
        list($key,$value) = explode(":: ", $line);
        $example[$key] = $value ;
    }
}

var_dump($example);
?>

Outputs: 输出:

array (size=4)
  'title' => string 'Article 2 Title' (length=15)
  'link' => string 'www.link.to/another_site' (length=24)
  'file' => string 'image2.png' (length=10)
  'description' => string 'Some more markdown text content describing another image' (length=56)

What I don't understand how to do is get the key/value pairs working using the first method, or, using the second method, loop through and return each content block with the key/value pairs in place. 我不明白该怎么做,是使用第一种方法使键/值对起作用,或者使用第二种方法,遍历并返回每个包含键/值对的内容块。

(My expected output: (我的预期输出:

array (size=2)
  0 => 
    array (size=4)
      'title' => string 'Article 1 Title' (length=15)
      'link' => string 'www.link.to/a_site' (length=18)
      'file' => string 'image1.png' (length=10)
      'description' => string 'Some markdown text content describing the image' (length=47)
  1 => 
    array (size=4)
      'title' => string 'Article 2 Title' (length=15)
      'link' => string 'www.link.to/another_site' (length=24)
      'file' => string 'image2.png' (length=10)
      'description' => string 'Some more markdown text content describing another image' (length=56)

Try using this kind of loop : 尝试使用这种循环:

$result = array();
$file = explode("====", file_get_contents("content/example.md"));
foreach ( $file as $content ) {
    $example = array();
    foreach(array_filter(array_map("trim",explode("----", $content))) as $line)
    {
        list($key,$value) = explode(":: ", $line);
        $example[$key] = $value ;
    }
    $result[] = $example;
}
var_dump($result);

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

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