Last Updated: February 25, 2016
·
672
· raphaelstolt

Turn a string structure into an array structure

In case you want to turn a string structure into an array structure, the following code snippet might come in handy (which doesn't use the ugly reference operator).

<?php 
$structure = 'a/b/c = 14';
list($structure, $value) = explode('=', $structure);

$structureElements = explode('/', trim($structure));
$data = trim($value);

foreach (array_reverse($structureElements) as $key) {
    $data = array($key => $data);
}

Which results in the following array structure:

array(1) {
  'a' =>
  array(1) {
    'b' =>
    array(1) {
      'c' =>
      int(14)
    }
  }
}