按parent_id,parent_id~> page_id,然后是page_order对stdClass进行排序

时间:2016-07-22 15:02:01

标签: php arrays sorting stdclass

我正在尝试对导航菜单列表进行排序,我正在提供(作为数组中的$响应)。因此,它将通过parent_id(null是顶层导航。)然后子页面将落入父ID,即page_id然后在page_order中。基本上按照您将看到导航的顺序。网站上的菜单。

例如:

First Nav
--First Nav Sub Page 1
--First Nav Sub Page 2
--First Nav Sub Page 3
--First Nav Sub Page 4
--First Nav Sub Page 5
Second Nav
Third Nav
--Third Nav Sub Page 1
--Third Nav Sub Page 2
Fourth Nav

数组:

Array (
[0] => stdClass Object
    (
        [page_id] => 122021
        [title] => First Nav
        [page_order] => 1
        [parent_id] => null
    )

[1] => stdClass Object
    (
        [page_id] => 99122
        [title] => Second Nav
        [page_order] => 2
        [parent_id] => null
    )

[2] => stdClass Object
    (
        [page_id] => 260447
        [title] => Third Nav
        [page_order] => 3
        [parent_id] => null
    )

[3] => stdClass Object
    (
        [page_id] => 170627
        [title] => Fourth Nav
        [page_order] => 4
        [parent_id] => null
    )

[4] => stdClass Object
    (
        [page_id] => 134788
        [title] => Third Nav Sub Page 1
        [page_order] => 1
        [parent_id] => 260447
    )

[5] => stdClass Object
    (
        [page_id] => 157074
        [title] => Third Nav Sub Page 2
        [page_order] => 2
        [parent_id] => 260447
    )

[6] => stdClass Object
    (
        [page_id] => 699423
        [title] => First Nav Sub Page 1
        [page_order] => 1
        [parent_id] => 122021
    )

[7] => stdClass Object
    (
        [page_id] => 346209
        [title] => First Nav Sub Page 2
        [page_order] => 2
        [parent_id] => 122021
    )

[8] => stdClass Object
    (
        [page_id] => 732773
        [title] => First Nav Sub Page 3
        [page_order] => 3
        [parent_id] => 122021
    )

[9] => stdClass Object
    (
        [page_id] => 164338
        [title] => First Nav Sub Page 4
        [page_order] => 4
        [parent_id] => 122021
    )

[10] => stdClass Object
    (
        [page_id] => 671117
        [title] => First Nav Sub Page 5
        [page_order] => 5
        [parent_id] => 122021
    )
)

我已经设法通过使用下面提到的ArrSort函数的parent_id然后是page_order(如上面的数组中所示)以部分顺序得到它,但是我无法将子页面page_order'放在'parent_id'的下面。

我试过了:

function ArrSort ($Array){
   $parent_id = array();
   $page_order = array();
   foreach($Array as $key => $value){
       $parent_id[] = $value->parent_id;
       $page_order[] = $value->page_order;
   }
   array_multisort($parent_id, SORT_ASC, $page_order, SORT_ASC, $Array);
   return $Array;
}
$sorted_pages_array = ArrSort($response);

我也尝试了以下内容,

function PageList($a, $b){
  if ( $a->page_id == $b->page_id ) {
    return 0;
  } else if ( $a->parent_id ) {
      if ( $a->parent_id == $b->parent_id ) {
         return ( $a->page_id < $b->page_id ? -1 : 1 );
      } else {
         return ( $a->parent_id >= $b->page_id ? 1 : -1 );
      }
  } else if ( $b->parent_id ) {
    return ( $b->parent_id >= $a->page_id ? -1 : 1);
  } else {
    return ( $a->page_id < $b->page_id ? -1 : 1 );
  }
}
usort($response, "PageList");

两者结合:

$sorted_pages_array = ArrSort($response);
usort($sorted_pages_array, "PageList");

不幸的是,我不熟悉数组和排序,并且可以使用一些帮助。

2 个答案:

答案 0 :(得分:0)

试试这个:

usort($sorted_pages_array, function($a, $b){
    if($a->parent_id === $b->parent_id && $a->page_id === $b->page_id){
        return $a->page_order - $b->page_order;
    }
    elseif($a->parent_id === $b->parent_id){
        return $a->page_id - $b->page_id;
    } else {
        return $a->parent_id - $b->parent_id;
    }
});

首先在数据库中对数据进行排序可能会更容易。

答案 1 :(得分:0)

做了一些挖掘后,我发现了这个: https://stackoverflow.com/a/8021033/3925032

因此,除了更改以下内容之外,还有以上链接的解决方案:

$node['object']->id$node['object']->page_id

$node['object']->top_id$node['object']->parent_id

$node['object']->name$node['object']->title

(并添加了一些我自己的更新),这就是我想要的。当我在函数ArrSort之后添加它(在我最初的问题中)时,一切都很好!