1

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)
Community
  • 1
  • 1
  • 3
    Can't understand what is the problem with 2nd method ? what do you want to change from it ? can you write a sample output you wish ? – Random Jul 30 '15 at 12:09
  • Pass a key to the outer foreach, and use that when adding to your `$example` array: `foreach ($file as $i => $content) { ... $example[$i][$key] = $value;` – billyonecan Jul 30 '15 at 12:27

1 Answers1

0

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);
Random
  • 3,158
  • 1
  • 15
  • 25