PHP 使用不同的排序字段创建数组


PHP create array with different sort fields

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

现在。。我有 2 个表格:页面和部分每个都有自己的 ID。页面有一个或多个部分。一个部分正好属于 1 页。两个 id 的列表序列在不同的字段中处理。

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

以下是检索数据后的数组示例:

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是同一父级关卡内的页面序列。

我在这里已经找到了一些循环示例,但是 - 说实话 - 我无法根据我的需求调整它们。而且,我需要一个回收循环吗?

我的数组的键应该是什么:section_id,因为它在所有页面上都是唯一的?如何进行正确的排序?

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

所以我需要的是:

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

任何强大的帮助和想法?

提前致谢沃尔夫冈

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

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

1) 创建要处理的所有页面的列表。生成的$key在第一级(如"1"),在第二级(如"1.1"),在第三级(如"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) 对部分列表进行排序 纳特 ( $sectionSortList );

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

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

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

最后一位保存的瘾君子会发现很多代码优化和减少的地方。但是,我喜欢带有 : 和 endif 的替代功能;;-)

如果有人看到有可能让事情变得更快,欢迎提供您的想法。