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?

2
  • 2
    JSON is your friend for this project. Commented Apr 11, 2012 at 17:36
  • @BrendonCheves: I disagree, JSON isn't exactly human friendly if you consider (lack of) whitespace, Unicode, escape sequences, braces... Commented Apr 11, 2012 at 23:05

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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"}]}

1 Comment

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. JSON ignores whitespace as well (good for human input) Oh, and I have no problem with Unicode either.
1

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

There are a few PHP implementations available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.