php 2d数组排序父子

时间:2015-05-30 14:33:51

标签: php arrays sorting parent


是的,是的我知道,这里有一堆关于数组排序的问题,但我需要一个更具体的解释 我有一个DB表来存储产品的多级菜单 例如:itemId,parentId,itemtitle,...
我使用这个表在网页上构建菜单,使用递归函数,我想使用这个DB表来获取后端html表单的数据,用于选项列表。 来自DB的未排序数据我需要正确排序并放入json数组。 我完成了半部分工作:

//empty arrays for sorting purposes  
$ids = array();
$parents = array();
// iterate, $items - unsorted array from DB 
foreach ($items as $key => $row) {
    $ids[$key]  = $row['itemId'];
    $parents[$key] = $row['parentId'];
}
// sorting
array_multisort($ids, SORT_ASC, $parents, SORT_ASC, $items);
//now $items partly sorted

图1
所以$ items列表看起来像:
(itemId - parentId - itemTitle)
1 - 0 - *** A
2 - 0 - *** B
3 - 0 - *** C
4 - 0 - *** D
6 - 1 - *** E
7 - 1 - *** F
8 - 1 - *** G
9 - 2 - *** H
10 - 2 - ***我 11 - 2 - *** J
12 - 2 - *** K
13 - 2 - *** L
14 - 3 - *** M
15 - 3 - *** N
16 - 3 - *** O
17 - 4 - *** P
18 - 4 - *** Q

图2
我需要:
1 - 0 - *** A
6 - 1 - *** E
7 - 1 - *** F
8 - 1 - *** G
2 - 0 - *** B
9 - 2 - *** H
10 - 2 - ***我 11 - 2 - *** J
12 - 2 - *** K
13 - 2 - *** L
3 - 0 - *** C
14 - 3 - *** M
15 - 3 - *** N
16 - 3 - *** O
4 - 0 - *** D
17 - 4 - *** P
18 - 4 - *** Q

所以问题是 - 如何将图1中的数组转换为图2中的数组?

所以这就是我的意思。仅限2级菜单:

    //get all categories, primary sorted by MySQL
    $items = $cataloAR->query("SELECT itemId, parentId,itemTitle FROM `catalog` ORDER BY parentId, itemId"); 
//pre-sort by - itemId, parentId
    $ids = array();//array for id`s
    $parents = array();//array for parents
// get columns list
    foreach ($items as $key => $row) {
        $ids[$key]  = $row['itemId'];
        $parents[$key] = $row['parentId'];
    }
// sort ascending by id, ascending by parent
    array_multisort($ids, SORT_ASC, $parents, SORT_ASC, $items);
//make emptyarray for sorteddata, like - $entryList['itemId']['itemTitle'] 
    $entryList = array();//empty array
    $len = count($items); //count elements for proceeding
    $currentEntry = 0;  //current element number,start from 0-element
//iterate
//while reaching last element in list
   while($currentEntry < $len){        
//if current element is empty => go to next element
        if (!isset($items[$currentEntry])) {
            $currentEntry++;
        } 
 //get current element from $items
//put it to $entryList
        $entryList[] = $items[$currentEntry];    
// get current id 
        $currentId = $items[$currentEntry]['itemId'];        
//remove element from  $items, because we just put it to the sorted list
        unset($items[$currentEntry]);     
//search in $items entries with  patternId = $items['itemId']  
//put to sorted list
//remove element 
        foreach ($items as $key => $value) {
            if($value['parentId'] == $currentId){
                $entryList[] = $value;               
                unset($items[$key]);                  
            } 
            if (!isset($value)){
                continue;
            }

        } 
//increase counter               
            $currentEntry++;                   
    }    
//purge sorted list from empty elements  $entryList
    $sortedList = array_filter($entryList);
 //Enjoy it!
    return $sortedList;

1 个答案:

答案 0 :(得分:0)

我对你的数组做了一些假设,并用下面的代码创建了它。我能够通过创建两个新数组($ root和$ menu)来复制所需的输出,然后循环遍历每个数组以构建菜单。这很棘手,最好的答案实际上是从一开始就以不同的方式构建这个数组,但如果这不可能,那么这段代码应该可以让你到达你需要的位置:

$items[] = array('itemId' => 1,'parentId' => 0,'itemTitle' => '***A');
$items[] = array('itemId' => 2,'parentId' => 0,'itemTitle' => '***B');
$items[] = array('itemId' => 3,'parentId' => 0,'itemTitle' => '***C');
$items[] = array('itemId' => 4,'parentId' => 0,'itemTitle' => '***D');
$items[] = array('itemId' => 6,'parentId' => 1,'itemTitle' => '***E');
$items[] = array('itemId' => 7,'parentId' => 1,'itemTitle' => '***F');
$items[] = array('itemId' => 8,'parentId' => 1,'itemTitle' => '***G');
$items[] = array('itemId' => 9,'parentId' => 2,'itemTitle' => '***H');
$items[] = array('itemId' => 10,'parentId' => 2,'itemTitle' => '***I');
$items[] = array('itemId' => 11,'parentId' => 2,'itemTitle' => '***J');
$items[] = array('itemId' => 12,'parentId' => 2,'itemTitle' => '***K');
$items[] = array('itemId' => 13,'parentId' => 2,'itemTitle' => '***L');
$items[] = array('itemId' => 14,'parentId' => 3,'itemTitle' => '***M');
$items[] = array('itemId' => 15,'parentId' => 3,'itemTitle' => '***N');
$items[] = array('itemId' => 16,'parentId' => 3,'itemTitle' => '***O');
$items[] = array('itemId' => 17,'parentId' => 4,'itemTitle' => '***P');
$items[] = array('itemId' => 18,'parentId' => 4,'itemTitle' => '***Q');

foreach ($items as $itemArray) {
    $parent = $itemArray['parentId'];
    $item = $itemArray['itemId'];
    $title = $itemArray['itemTitle'];

    if ($parent == 0) {
        // this is a root item
        $root[$item] = $title;
    } else {
        // this is a submenu item
        $menu[$parent][$item] = $title;
    }
}

foreach ($root as $key => $value) {
    // this is a root item, which is always 0.
    echo "$key, 0, $value <br/>";
    foreach ($menu as $parent => $item) {
        if ($parent == $key) {
            foreach ($item as $itemId => $title) {
                // output the submenu items under the root
                echo "$itemId, $parent, $title <br />";
            }
        }
    }
}