Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to represent a multilevel array in a string form

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?

like image 726
camcam Avatar asked Apr 21 '26 20:04

camcam


2 Answers

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

like image 120
Sandeep Rajoria Avatar answered Apr 23 '26 15:04

Sandeep Rajoria


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"}]}
like image 31
orourkek Avatar answered Apr 23 '26 14:04

orourkek