3

This is an example of a mixed, multi-level, irregular array in php:

$settings['style_formats'] = array(
  array('title' => 'Center table', 'selector' => 'table', 'styles' => array('margin-left' => 'auto', 'margin-right' => 'auto')),
  array('title' => 'Menu style', 'selector' => 'ul,ol', 'classes' => 'menu'),
  array('title' => 'Layer2', 'inline' => 'div', 'styles' => array('background-color' => 'orange')),
  array('title' => 'Bold text', 'inline' => 'b'),
  array('title' => 'Red text', 'inline' => 'span', 'styles' => array('color' => '#ff0000')),
  array('title' => 'Red header', 'block' => 'h1', 'styles' => array('color' => '#ff0000')),
  array('title' => 'Example 1', 'inline' => 'span', 'classes' => 'example1'),
  array('title' => 'Example 2', 'inline' => 'span', 'classes' => 'example2'),
  array('title' => 'Table styles'),
  array('title' => 'Table row 1', 'selector' => 'tr', 'classes' => 'tablerow1'),
);

I need to find a method to represent and translate this kind of array from string format to php array. The initial string format must be readable and writeable by a human using a text editor. So e.g. it should not be a result of using "serialize", because "serialize" is a php function (and rather not possible to create by a human) and the string must be possible to create manually in a text editor.

The string will be passed as parameter to a function which will translate it to a php array like the one above.

If it was a simple array, I would use a comma separated string and "explode". But it is multilevel, so using "explode" won't work as it will split internal arrays. preg_split also doesn't look promising because the array is very irregular.

Any ideas how to do it?

camcam
  • 2,585
  • 8
  • 49
  • 65

3 Answers3

2

How about trying to translate this into an xml document, would be human and machine readable both?

There are many php libraries to do so and read the xml file into php array...

For a primer

http://www.phpbuilder.com/columns/adam_delves20060206.php3

http://www.php.net/manual/en/book.simplexml.php

Sandeep Rajoria
  • 1,243
  • 8
  • 14
1

JSON is one of many solutions, like so: (forgive me for no formatting)

{"style_formats":[{"title":"Center table","selector":"table","styles":{"margin-left":"auto","margin-right":"auto"}},{"title":"Menu style","selector":"ul,ol","classes":"menu"},{"title":"Layer2","inline":"div","styles":{"background-color":"orange"}},{"title":"Bold text","inline":"b"},{"title":"Red text","inline":"span","styles":{"color":"#ff0000"}},{"title":"Red header","block":"h1","styles":{"color":"#ff0000"}},{"title":"Example 1","inline":"span","classes":"example1"},{"title":"Example 2","inline":"span","classes":"example2"},{"title":"Table styles"},{"title":"Table row 1","selector":"tr","classes":"tablerow1"}]}
orourkek
  • 2,091
  • 15
  • 22
  • JSON works best in my case. The notation is very similar to native php associative array notation (for me it's a plus). Also, as opposite to simplexml, it requires exactly 1 line of code, while importing xml to nested assoc array required some custom code like [this](http://stackoverflow.com/a/6237999/523972). JSON ignores whitespace as well (good for human input) Oh, and I have no problem with Unicode either. – camcam Apr 12 '12 at 09:44
1

If this is to be written by humans, YAML might be your best bet.

There are a few PHP implementations available.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500