Php 数组中的类别层次结构(类别id=>;父id)
我试图从一个简单的数组中创建一个多维数组层次结构,该数组包含类别ID和父ID对。类别可以同时是父类别和子类别。基本类别的父类别为0(=无父类别)。例如:Php 数组中的类别层次结构(类别id=>;父id),php,arrays,multidimensional-array,hierarchy,Php,Arrays,Multidimensional Array,Hierarchy,我试图从一个简单的数组中创建一个多维数组层次结构,该数组包含类别ID和父ID对。类别可以同时是父类别和子类别。基本类别的父类别为0(=无父类别)。例如: # cat_id => parent_id $initialArray = array( 1 => 0, 2 => 1, 3 => 2, 4 => 0, 5 => 4, 6 => 0 ); 从这里,我想得到一个数组,它表示如下结构: $menu = array( 0
# cat_id => parent_id
$initialArray = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
从这里,我想得到一个数组,它表示如下结构:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
- 一,
- 二,
- 三,
- 二,
- 四,
- 五,
- 六,
$initialArray
的内容
我试着看其他类似的问题,但找不到答案。请帮忙 您可以创建这样的数组
$initialArray = array();
$initialArray[parent_id][cat_id]='your_value'; //$initialArray[0][0] ... and then increasing
您可以创建这样的数组
$initialArray = array();
$initialArray[parent_id][cat_id]='your_value'; //$initialArray[0][0] ... and then increasing
如果你想形成一个递归数组,你可以在这页上参考
这可能对你有很大帮助。谢谢。如果你想形成一个递归数组,你可以在本页上查阅
这可能对你有很大帮助。谢谢。是否有任何方法可以更改阵列的生成方式?正如你现在所拥有的,这将是一个更麻烦的问题,那么它值得你去尝试并产生你想要的 或者,您可以尝试生成如下内容:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
允许这样一个简单的循环:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
-
-
上面是模板语法,我相信我们都知道这是什么,所以我现在要解释一下
或者简单循环如下所示:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo“”;
foreach($parent=>$subs的菜单){
回声“- ”;
echo$parent;
回声“
”;
foreach($subs作为$index=>$val){
回声“- ”;
echo$val;
回声“
”;
}
回声“
”;
回声“ ”;
}
回声“
”;
是否有任何方法可以更改数组的生成方式?正如你现在所拥有的,这将是一个更麻烦的问题,那么它值得你去尝试并产生你想要的
或者,您可以尝试生成如下内容:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
允许这样一个简单的循环:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
-
-
上面是模板语法,我相信我们都知道这是什么,所以我现在要解释一下
或者简单循环如下所示:
$menu = array(
0 => array(0, 1, 2, 3),
1 => array(0, 1),
);
<ul>
<?php foreach ($menu as $parent => $subs) : ?>
<li>
<?php echo $parent; ?>
<ul>
<?php foreach ($subs as $index => $val): ?>
<li>
<?php echo $val; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
echo "<ul>";
foreach($menu as $parent => $subs){
echo "<li>";
echo $parent;
echo "<ul>";
foreach($subs as $index => $val) {
echo "<li>";
echo $val;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo“”;
foreach($parent=>$subs的菜单){
回声“- ”;
echo$parent;
回声“
”;
foreach($subs作为$index=>$val){
回声“- ”;
echo$val;
回声“
”;
}
回声“
”;
回声“ ”;
}
回声“
”;
获取您的列表:
<?php
$initialArray = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
$menus = array();
function generateMenus($start) {
global $initialArray;
foreach($initialArray as $k => $v) {
if($start == $v) {
$menus[] = $k;
}
}
return $menus;
}
$final = array();
foreach($initialArray as $key => $value) {
$final[$value] = generateMenus($value);
}
echo '<ul>';
function generateList($start) {
global $final;
echo '<li>'.$start.'</li>';
if(!empty($final[$start])) {
echo '<ul>';
foreach($final[$start] as $v) {
generateList($v);
}
echo '</ul>';
}
}
generateList(0);
echo '</ul>';
?>
获取您的列表:
<?php
$initialArray = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
$menus = array();
function generateMenus($start) {
global $initialArray;
foreach($initialArray as $k => $v) {
if($start == $v) {
$menus[] = $k;
}
}
return $menus;
}
$final = array();
foreach($initialArray as $key => $value) {
$final[$value] = generateMenus($value);
}
echo '<ul>';
function generateList($start) {
global $final;
echo '<li>'.$start.'</li>';
if(!empty($final[$start])) {
echo '<ul>';
foreach($final[$start] as $v) {
generateList($v);
}
echo '</ul>';
}
}
generateList(0);
echo '</ul>';
?>
我觉得你需要一个递归函数。假设所有内容都有一个父项或值,从基本级别0开始,我调整了数组的大小,使所有父项ID都列出了它们的子项,而不是上面的相反方式。之后,我创建了一个递归函数
$initialArray = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
// resituate the array
$parent_ids = array();
foreach ($initialArray as $category_id => $parent_id) {
if (!isSet($parent_ids[$parent_id])) {
$parent_ids[$parent_id] = array();
}
$parent_ids[$parent_id][] = $category_id;
}
// end_array is the result
$end_array = array();
/**
* Takes the key of the parent, the current set that it's working off of, the list of parent ids for reference
* and the current place in the end result array, acting recursively
*/
function recursive($parent_key, $current_set, $parent_ids, $end_array) {
foreach ($current_set as $parent_value) {
if (!isSet($parent_ids[$parent_value])) {
$end_array[$parent_key][] = $parent_value;
} else {
// if the parent_value is found in parent_ids, pass those values to the same function and the current end_array position
$end_array[$parent_key] = recursive($parent_value, $parent_ids[$parent_value], $parent_ids, $end_array[$parent_key]);
}
}
return $end_array;
}
// start with the top most element
$end_array = recursive(key($parent_ids), current($parent_ids), $parent_ids, $end_array);
print '<pre>'.
print_r($parent_ids, true).
print_r($end_array,true).
'</pre>'
;
我觉得你需要一个递归函数。假设所有内容都有一个父项或值,从基本级别0开始,我调整了数组的大小,使所有父项ID都列出了它们的子项,而不是上面的相反方式。之后,我创建了一个递归函数
$initialArray = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
// resituate the array
$parent_ids = array();
foreach ($initialArray as $category_id => $parent_id) {
if (!isSet($parent_ids[$parent_id])) {
$parent_ids[$parent_id] = array();
}
$parent_ids[$parent_id][] = $category_id;
}
// end_array is the result
$end_array = array();
/**
* Takes the key of the parent, the current set that it's working off of, the list of parent ids for reference
* and the current place in the end result array, acting recursively
*/
function recursive($parent_key, $current_set, $parent_ids, $end_array) {
foreach ($current_set as $parent_value) {
if (!isSet($parent_ids[$parent_value])) {
$end_array[$parent_key][] = $parent_value;
} else {
// if the parent_value is found in parent_ids, pass those values to the same function and the current end_array position
$end_array[$parent_key] = recursive($parent_value, $parent_ids[$parent_value], $parent_ids, $end_array[$parent_key]);
}
}
return $end_array;
}
// start with the top most element
$end_array = recursive(key($parent_ids), current($parent_ids), $parent_ids, $end_array);
print '<pre>'.
print_r($parent_ids, true).
print_r($end_array,true).
'</pre>'
;
我知道,晚会迟到了,但看起来很有趣
/**
* Insert: Find the 'parent' node
* if child not found then insert a 'node'
*
* @param array node passed by reference as the new node will be inserted
* @param integer $parentId - root Id must be the lowest value
* @param integer $childId
* @return boolean true if parentId found and processed
*/
function insertNode(&$node, $parentId, $childId) {
if (isset($node[$parentId])) { // this node will be processed
if (!isset($node[$parentId][$childId])) {
$node[$parentId][$childId] = array(); // add child node
return true;
}
return true; // end of processing
}
// check all the children of this node...
foreach($node as &$child) { // need the reference
if (insertNode($child, $parentId, $childId)) {
return true;
}
}
return false; // parentId not in the tree
}
它将从根节点开始一次插入一个节点。对于大树来说,它可能非常昂贵
工作示例位于:
完成工作的例行程序:
The node to be processed is passed by reference.
The processing will end when the 'parent' id is found
注:
$theTree = array(current($links) => array()); // root
// insert all the child nodes into the tree
foreach($links as $childId => $parentId) {
$inserted = insertNode($theTree, $parentId, $childId);
}
// output the tree
echo '<pre>', 'Children are in the same order as the input array.', '<br />';
print_r($theTree);
echo '</pre>';
给定的输入节点列表是'child'=>'parent'顺序,这是不寻常的,这是正常的,只需记住在处理过程中
处理输入数据:
# cat_id => parent_id
$links = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4, // multiple children
11 => 4,
99 => 4,
13 => 11,
6 => 0
);
输出时,我在原始输入中添加了一个子树
我知道,晚会迟到了,但看起来很有趣
/**
* Insert: Find the 'parent' node
* if child not found then insert a 'node'
*
* @param array node passed by reference as the new node will be inserted
* @param integer $parentId - root Id must be the lowest value
* @param integer $childId
* @return boolean true if parentId found and processed
*/
function insertNode(&$node, $parentId, $childId) {
if (isset($node[$parentId])) { // this node will be processed
if (!isset($node[$parentId][$childId])) {
$node[$parentId][$childId] = array(); // add child node
return true;
}
return true; // end of processing
}
// check all the children of this node...
foreach($node as &$child) { // need the reference
if (insertNode($child, $parentId, $childId)) {
return true;
}
}
return false; // parentId not in the tree
}
它将从根节点开始一次插入一个节点。对于大树来说,它可能非常昂贵
工作示例位于:
完成工作的例行程序:
The node to be processed is passed by reference.
The processing will end when the 'parent' id is found
注:
$theTree = array(current($links) => array()); // root
// insert all the child nodes into the tree
foreach($links as $childId => $parentId) {
$inserted = insertNode($theTree, $parentId, $childId);
}
// output the tree
echo '<pre>', 'Children are in the same order as the input array.', '<br />';
print_r($theTree);
echo '</pre>';
给定的输入节点列表是'child'=>'parent'顺序,这是不寻常的,这是正常的,只需记住在处理过程中
处理输入数据:
# cat_id => parent_id
$links = array(
1 => 0,
2 => 1,
3 => 2,
4 => 0,
5 => 4, // multiple children
11 => 4,
99 => 4,
13 => 11,
6 => 0
);
输出时,我在原始输入中添加了一个子树
我将是那个家伙-你们试过什么?你们需要递归函数。我可能是那个家伙的副本-你们试过什么?你们需要递归函数。可能是的副本,但数组中可能有无限的深度。那么,你将如何仅仅通过两个循环来实现这一点呢?好吧,如果我知道有多少个循环,我只需要简单地重复一下就可以了。但是正如上面manish所说的,我在我的问题中解释过,我不知道初始数组的内容,只知道它的格式。谢谢你清理了它,Leandro,我昨天没有睡觉!但是一个数组中可能有无限的深度。那么,你将如何仅仅通过两个循环来实现这一点呢?好吧,如果我知道有多少个循环,我只需要简单地重复一下就可以了。但是正如上面manish所说的,我在我的问题中解释过,我不知道初始数组的内容,只知道它的格式。谢谢你清理了它,Leandro,我昨天没有睡觉!谢谢这很好用。我与辞职的方向是一致的,但我被第二部分卡住了。:)不是“[5]=>6”项,而是“[6]=>array()”,即空数组?不,我认为这很好,因为6没有子项。3号和5号也没有孩子,也不是ArrayShanks!这很好用。我与辞职的方向是一致的,但我被第二部分卡住了。:)应该是“[6]=>array()”而不是“[5]=>6”条目,即空数组吗?不,我想我