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)
}
}
}
Written by Raphael Stolt
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Related Tags
#php
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#