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

时间:2015-07-30 12:07:05

标签: php arrays text-files

我在文本文件中有一个内容,分成块,这些块进一步分为字段/值对。例如:

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

使用此方法(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);

?>

我将每个内容块放入一个数组中,但不是键/值对:

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)

该答案中给出的第二个示例成功分割了我的键/值对:

<?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);
?>

输出:

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)

我不明白怎么做是使用第一种方法获取键/值对,或者使用第二种方法循环并返回每个内容块和键/值对。

(我的预期输出:

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)

1 个答案:

答案 0 :(得分:0)

尝试使用这种循环:

$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);
相关问题