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?
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
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"}]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With