[英]Multidimension PHP array parent tack value
I've been working on some code today where I got stuck at a little multidimensional array problem. 我今天一直在研究一些代码,但遇到了一些多维数组问题。 First of all it's maybe handy to read some code I wrote to get a better view on the problem itself: 首先,阅读一些我编写的代码可能会很方便,以便更好地了解问题本身:
public function treeLeaves(array $elements, $parent = 0) {
$branch = array();
foreach($elements as $element) {
$leaf = array('pageid' => $element['pageid'],
'page_parent' => $element['page_parent'],
'label' => ucfirst($element['page_print'][0]['print_title']),
'uri' => $element['page_alias']);
if($element['page_parent'] == $parent) {
$children = $this->treeLeaves($elements, $leaf['pageid']);
if($children) {
foreach($children as $key => $child) {
$leaf['pages'][] = $children[$key];
}
}
$branch[] = $leaf;
}
}
return $branch; }
For some reason I can't figure out how to glue the parent URI alias onto all the separate child URIs. 由于某些原因,我无法弄清楚如何将父URI别名粘贴到所有单独的子URI上。 The desired result I'm looking for should look something like this: http://pastebin.com/Eh9ExBjG 我正在寻找所需的结果应类似于以下内容: http : //pastebin.com/Eh9ExBjG
I hope some master can help me out here. 我希望一些主人可以在这里帮助我。 I've been trying so many different stuff, but can't figure this thing out, even though I feel that it is relatively easy to solve. 我一直在尝试许多不同的东西,但是即使我觉得它相对容易解决,也无法弄清楚。
Somewhat simplified, but I think you would get the idea: 略有简化,但我想您会明白的:
function treeLeaves($elements, $parent = 0, $baseUri = '/index') {
$branch = array();
foreach($elements as $element) {
if ($element['page_parent'] == $parent) {
$leaf = array(
'uri' => $baseUri . '/' . $element['page_alias'];
);
$leaf['pages'] = treeLeaves($elements, $element['pageid'], $leaf['uri']);
$branch[] = $leaf;
}
}
return $branch;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.