创建使用从MySQL递归PHP数组 [英] Creating an array using recursive php from mysql
问题描述
我需要从像这样组织的mysql数据库创建一个数组
ID描述parentId的
1级1 0
2级2 0
3 1A级1
4 1b级1
5级1A1 3
6级1a1a 5
这样的输出是这样的:
等级1
1A级
1A1级
等级1a1a
1B级
2级
不过我现在的code只输出到第二级,然后尽一切别的孩子是自己的父母。下面是目前的code:
$查询=SELECT * FROM pB_test ORDER BY parentId的ASC;
$结果= mysql_query($查询)或死亡(数据库错误('mysql_errno()')mysql_error());$树=阵列();而($行= mysql_fetch_assoc($结果))
{
如果($行['parentId的'] == 0)
{
$行['儿童'] =阵列();
$树[$行['身份证'] =阵列(
'ID'=> $行['身份证'],
'描述'=> $行['说明'],
parentId的'=> $行['parentId的']
);
}
其他
{
$树[$行['parentId的'] ['儿童'] [$行['身份证'] = $行['说明'];
}
}$数= array_keys($树);的foreach($算作$数组)
{
ksort($树[$阵列] ['儿童']);
}回声的print_r($树,真);
任何帮助或轻推在正确的方向将是巨大的。干杯
更新:工作code
$结果=阵列();
而($行= mysql_fetch_assoc($ dbresult))
{
$结果[] = $行; $树= NULL;
的foreach($结果$结果)
{
$ thisref =安培; $ refs-> {$结果[ID]};
的foreach($结果为$ K => $ V)
{
$ thisref-> {$}ķ= $ V;
}
如果($结果['parentId的'] == 0){
$树形> {$结果[ID]} =安培; $ thisref;
}其他{
$ refs-> {$结果['parentId的']} - >孩子 - > {$结果[ID]} =安培; $ thisref;
}
}$树; //包含新排序树。}的print_r($树);
我发现这个code为分组父子阵列是惊人的。我在4深处测试没有问题都没有。它不是一个递归函数虽然。
$树= NULL;
的foreach($结果$结果)
{
$ thisref =安培; $ refs-> {$结果[ID]};
的foreach($结果为$ K => $ V)
{
$ thisref-> {$}ķ= $ V;
}
如果($结果['parentId的'] == 0){
$树形> {$结果[ID]} =安培; $ thisref;
}其他{
$ refs-> {$结果['parentId的']} - >孩子 - > {$结果[ID]} =安培; $ thisref;
}
}$树; //包含新排序树。
您可能需要做一些修改让它完全的情况下工作。但基本上它遍历所有的结果,并通过引用合并它们。
请注意,结束 $树
数据类型是对象
而不是阵列
好运
更新
您可以创建数组这样
$查询=SELECT * FROM pB_test ORDER BY parentId的ASC;
$ dbresult =的mysql_query($查询)或死亡(。'数据库错误('mysql_errno()')mysql_error());$结果=阵列();
而($行= mysql_fetch_assoc($ dbresult))
{
$结果[] = $行
}
I need to create an array from a mysql database organized like so
id description parentId
1 Level 1 0
2 Level 2 0
3 Level 1a 1
4 Level 1b 1
5 Level 1a1 3
6 Level 1a1a 5
So that the output is like this:
Level 1
Level 1a
Level 1a1
Level 1a1a
Level 1b
Level 2
However my current code only outputs to the second level and then makes every other child it's own parent. Below is the current code:
$query = "SELECT * FROM pB_test ORDER BY parentId ASC";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
$tree = array();
while($row = mysql_fetch_assoc($result))
{
if($row['parentId'] == 0)
{
$row['Children'] = array();
$tree[$row['id']] = array(
'id' => $row['id'],
'description' => $row['description'],
'parentId' => $row['parentId']
);
}
else
{
$tree[$row['parentId']]['Children'][$row['id']] = $row['description'];
}
}
$count = array_keys($tree);
foreach ($count as $array)
{
ksort($tree[$array]['Children']);
}
echo print_r($tree, true);
Any help or nudge in the right direction would be great. Cheers
Update: Working Code
$results = array();
while($row=mysql_fetch_assoc($dbresult))
{
$results[]=$row;
$tree = null;
foreach($results as $result)
{
$thisref = &$refs->{$result['id']};
foreach($result as $k => $v)
{
$thisref->{$k} = $v;
}
if ($result['parentId'] == 0) {
$tree->{$result['id']} = &$thisref;
} else {
$refs->{$result['parentId']}->children->{$result['id']} = &$thisref;
}
}
$tree; // contains the newly sorted tree.
}
print_r($tree);
I found this code for grouping parent child arrays to be amazing. I have tested in 4 depths with no issue what so ever. It isn't a recursive function though.
$tree = null;
foreach($results as $result)
{
$thisref = &$refs->{$result['id']};
foreach($result as $k => $v)
{
$thisref->{$k} = $v;
}
if ($result['parentId'] == 0) {
$tree->{$result['id']} = &$thisref;
} else {
$refs->{$result['parentId']}->children->{$result['id']} = &$thisref;
}
}
$tree; // contains the newly sorted tree.
You may have to do some modification for it to fully work with your situation. But basically it loops through all the results and combines them by reference.
Do note that the ending $tree
data type is an object
and not an array
Good Luck
UPDATE
You can create the array as such
$query = "SELECT * FROM pB_test ORDER BY parentId ASC";
$dbresult = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
$results = array();
while($row=mysql_fetch_assoc($dbresult))
{
$results[]=$row
}
这篇关于创建使用从MySQL递归PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!