PHP创建具有不同排序字段的数组

时间:2013-02-03 20:01:58

标签: php sorting

首先感谢stackoverflow提供这个平台和你,给新手一个帮助; - )

现在...... 我有2个表:页面和部分 每个人都有自己的身份证。 页面包含一个或多个部分。 一节只属于1页。 两个id的列表序列在不同的字段中处理。

我读了两个表并创建了一个(未排序的)数组。 我最终需要的是如下所示的排序列表,其中page_id和section_id的顺序正确。

这是检索数据后我的tarray的一个例子:

myArr[] = array( page_id=>2,  section_id=>2,  parent_id=>0, level=>0, page_seq=>1, section_seq=>2, page_title=>p1 );
myArr[] = array( page_id=>2,  section_id=>9,  parent_id=>0, level=>0, page_seq=>1, section_seq=>1, page_title=>p1 );
myArr[] = array( page_id=>3,  section_id=>3,  parent_id=>0, level=>0, page_seq=>2, section_seq=>1, page_title=>p2 );
myArr[] = array( page_id=>4,  section_id=>4,  parent_id=>0, level=>0, page_seq=>3, section_seq=>1, page_title=>p3 );
myArr[] = array( page_id=>5,  section_id=>5,  parent_id=>3, level=>1, page_seq=>3, section_seq=>1, page_title=>p2-3 );
myArr[] = array( page_id=>6,  section_id=>6,  parent_id=>3, level=>1, page_seq=>2, section_seq=>1, page_title=>p2-2 );
myArr[] = array( page_id=>7,  section_id=>7,  parent_id=>4, level=>1, page_seq=>1, section_seq=>1, page_title=>p3-1 );
myArr[] = array( page_id=>8,  section_id=>8,  parent_id=>7, level=>2, page_seq=>1, section_seq=>1, page_title=>p3-1-1 );
myArr[] = array( page_id=>9,  section_id=>10, parent_id=>5, level=>2, page_seq=>1, section_seq=>1, page_title=>p2-1-1 );
myArr[] = array( page_id=>9,  section_id=>11, parent_id=>5, level=>2, page_seq=>1, section_seq=>2, page_title=>p2-1-1 );
myArr[] = array( page_id=>10, section_id=>12, parent_id=>3, level=>1, page_seq=>1, section_seq=>1, page_title=>p2-1 );

我的问题是排序。

  • section_seq是页面中该部分的序列。
  • page_seq是同一父级的级别内的页面序列。

我在这里找到了一些反复循环的例子,但是 - 说实话 - 我无法根据自己的需要调整它们。而且,我需要一个rekursive循环吗?

我的数组的关键是什么:section_id,因为它在所有页面上都是唯一的? 怎么做正确的排序?

需要注意的是:遗憾的是,不能使用页面标题 - 如上例所示 - 用于排序目的;-)因为它是自由文本...

所以我需要的是:

  • 读取第一页(1),级别为0,page_seq = 1
  • 读第一页(2),其中第1级 - 如果存在 - 第(1)页为父级,page_seq = 1
  • 读第一页(3),第2级 - 如果存在 - 页面(2)为父级,page_seq = 1
  • ...只要不存在更深层次,就继续
  • 读第二页(4),其中第2级 - 如果存在 - 第(2)页为父级,page_seq = 1
  • ...只要不存在更深层次,并且此页面上没有更多页面(第2页为父级)
  • 读第二页(5),其中第1级 - 如果存在 - 第(1)页为父级,page_seq = 1
  • ...只要没有更深层次存在,并且此页面上没有更多页面(第5页)作为父级
  • 读取第0页的第二页(6)和page_seq = 2
  • 等等。

任何强大的帮助&想法?

提前致谢 沃尔夫冈

2 个答案:

答案 0 :(得分:0)

我的想法是使用数据库进行排序。使用MySQL,您可以在两个表上使用左连接,以在一个结果表中获取节和页数据。在查询中,您可以使用ORDER BY关键字指定必须对结果进行排序的列。

答案 1 :(得分:0)

所以最后我找到了解决方案

1)创建要处理的所有页面的列表。生成的$ key在第一级看起来像“1”,在第二级看起来像“1.1”,在第3级看起来像“1.1.1”等。

read all pages
while ( $fetch_obj_pages = $obj_pages->fetchRow() ):
    // do some validations and keep only those in mind which should be processed
    ....

    // get and save key for sorting
    if ( $isValid == TRUE ):
        $key = '';
        if ( array_key_exists ( $fetch_obj_pages[ 'parent_id' ],  $pageSortList ) == TRUE ):
            $key = $pageSortList[ $fetch_obj_pages[ 'parent_id' ]] . '.';
        endif;
        $key .= $fetch_obj_pages[ 'page_seq' ];
        $pageSortList[ $fetch_obj_pages[ 'page_id' ]] = $key;
     endif;
endwhile;

2)创建要处理的所有部分的列表。它取自上面页面列表中生成的密钥,并使用部分排序顺序对其进行增强,然后将每个section_id保存为密钥。

read all sections
while ( $fetch_obj_sections = $obj_sections->fetchRow() ):
    // do some validations and keep only those in mind which should be processed
    ....

    // get and save key for sorting
    if ( $isValid == TRUE ):
        // get key from page and save key for section
        $key = '';
        if ( array_key_exists ( $fetch_obj_sections[ 'page_id' ],  $pageSortList ) == TRUE ):
            $key = $pageSortList[ $fetch_obj_sections[ 'page_id' ]] . '-';
        endif;
        $key .= $fetch_obj_sections[ section_seq ];
        $sectionSortList[ $fetch_obj_sections[ 'section_id' ]] = $key;
    endif;
endwhile;

3)对部分列表进行排序     natsort($ sectionSortList);

4)通过循环$ sectionSortList并获取必要的数据来构建结果,因为section_id被用作此列表中的键。

在有人问之前:当然,所有可能的过滤器都在SQL中完成。但有些是不可能的,因为需要在循环中完成 -

最有可能的是,步骤1)和2)可以使用JOIN和正确的ORDER BY一步完成。

Last-Bit-Saving-Junkies会找到许多代码优化和减少的地方。 但是,我喜欢以下的替代功能:和endif; ; - )

如果有人认为有可能让事情变得更快,那么欢迎提出您的想法。