I have an array like this one:
$array = array(
array('id' => 'foo.bar'),
array('id' => 'foo'),
array('id' => 'foo.baz.bar'),
array('id' => 'foo.bar.bar'),
);
I can split
the id
fields to get them as paths, and then I'd like to sort them into a tree... I tried this:
$result = array();
foreach($array as $element) {
$path = explode('.', $element['id']);
$subtree = $result;
while(!empty($path)) {
$path_element = array_shift($path);
if(empty($path_element)) {
$subtree['data'] = $element;
} else {
if(!is_array($subtree[$path_element])) {
$subtree[$path_element] = array();
}
$subtree = $subtree[$path_element];
}
}
}
But all I get is a load of warnings and an empty $res
-Array.
PHP Notice: Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: baz in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
PHP Notice: Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP 1. {main}() tree.php:0
(Line 24 is $s = $s[$pe];
)
Any hint?
EDIT: My desired output would be like this
$res = array(
'foo' => array(
'data' => ...
'bar' => array(
'data' => ...
'bar' => array(
'data' => ...
),
),
'baz' => array(
'bar' => array(
'data' => ...
),
),
),
);
The data
elements are the original elements from the array.
The code below generates the following result:
Array
(
[foo] => Array
(
[data] => ...
[baz] => Array
(
[bar] => Array
(
[data] => ...
)
)
[bar] => Array
(
[bar] => Array
(
[data] => ...
)
)
)
)
I renamed some of you're variables...
$array = array(
array('id' => 'foo.bar'),
array('id' => 'foo'),
array('id' => 'foo.baz.bar'),
array('id' => 'foo.bar.bar'),
);
$res = array();
foreach($array as $e) {
$parts = explode('.', $e['id']);
$temp = &$res;
foreach($parts as $key => $el) {
if (!isset($temp[$el])) $temp[$el] = array();
if ($key == count($parts)-1) $temp[$el] = array('data' => '...');
$temp = &$temp[$el];
}
}
print_r($res);
Make $s referencing $res, using the &
. This will pass the data by reference instead of by value.
$a = array(
array('id' => 'foo.bar'),
array('id' => 'foo'),
array('id' => 'foo.baz.bar'),
array('id' => 'foo.bar.bar'),
);
$res = array();
foreach($a as $e) {
$p = explode('.', $e['id']);
$s = &$res;
while(!empty($p)) {
$pe = array_shift($p);
if(empty($p)) {
$s['data'] = $e;
} else {
if(!isset($s[$pe])) {
$s[$pe] = array();
}
$s = &$s[$pe];
}
}
}
echo '<pre>';
print_r($res);
echo '</pre>';
results in
Array
(
[foo] => Array
(
[data] => Array
(
[id] => foo.bar
)
[baz] => Array
(
[data] => Array
(
[id] => foo.baz.bar
)
)
[bar] => Array
(
[data] => Array
(
[id] => foo.bar.bar
)
)
)
[data] => Array
(
[id] => foo
)
)
Recursion is your answer.
and you would need something along the lines of:
<?php
$a = array(
array('id' => 'foo.bar'),
array('id' => 'foo'),
array('id' => 'foo.baz.bar'),
array('id' => 'foo.bar.bar'),
);
function createTree($path) {
if ( count($path) === 1 ) {
return array($path[0]=>array('data'=>'some random data here'));
} else {
$currentRoot = array_shift($path);
return array($currentRoot => createTree($path));
}
}
$result = array();
foreach ( $a as $item ) {
$result = array_merge_recursive(
$result,
createTree(
explode('.',$item['id'])
)
);
}
// $result now is what you wanted