PHP-创建层次结构数组 [英] PHP - Create Hierarchal Array

查看:71
本文介绍了PHP-创建层次结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不知道如何开始表达这个问题,但是基本上,我有一个看起来像这样的数组:

I'm not even sure how to begin wording this question, but basically, I have an array, that looks like this:

Array
(
    [0] => /
    [1] => /404/
    [2] => /abstracts/
    [3] => /abstracts/edit/
    [4] => /abstracts/review/
    [5] => /abstracts/view/
    [6] => /admin/
    [7] => /admin/ads/
    [8] => /admin/ads/clickcounter/
    [9] => /admin/ads/delete/
    [10] => /admin/ads/edit/
    [11] => /admin/ads/list/
    [12] => /admin/ads/new/
    [13] => /admin/ads/sponsordelete/
    [14] => /admin/ads/sponsoredit/
    [15] => /admin/ads/sponsornew/
    [16] => /admin/ads/stats/
    [17] => /admin/boilerplates/
    [18] => /admin/boilerplates/deleteboiler/
    [19] => /admin/boilerplates/editboiler/
    [20] => /admin/boilerplates/newboilerplate/
    [21] => /admin/calendar/event/add/
    [22] => /admin/calendar/event/copy/
)

我需要减少 /'将其处理为如下所示的数组:

And I need to 'reduce' / 'process' it into an array that looks like this:

Array
(
    [''] => Array()
    ['404'] => Array()
    ['abstracts'] => Array
                     (
                          [''] => Array()
                          ['edit'] => Array()
                          ['review'] => Array()
                          ['view'] => Array()
                     )
    ['admin'] => Array
                     (
                          ['ads'] => Array
                                     (
                                         [''] => Array()
                                         ['clickcounter'] => Array()
                                         ['delete'] =>Array()
                                         ['edit'] => Array()
                                     )
                     )
    .....
    .....   
)

如果手动初始化,则将看起来像这样:

That, if manually initialized would look something like this:

$urlTree = array( ''         => array(),
                  '404'      => array(),
                  'abstracts'=> array( ''      => array(),
                                       'edit'  => array(),
                                       'review'=> array(),
                                       'view'  => array() ),
                  'admin'    => array( 'ads'=> array( ''            => array(),
                                                      'clickcounter'=> array(),
                                                      'delete'      => array(),
                                                      'edit'        => array() ) )
);

我通常会避开直接询问SO上的大量代码,但有人可能会任何可以遍历我的数组并将其转换为层次结构的建议/代码?

I usually stray away from asking straight up for a chunk of code on SO, but does anyone perhaps have any advice / code that can traverse my array and convert it to a hierarchy?

编辑:这是我现在拥有的功能,其中,我知道它很小,我今天似乎没什么意思。

Here is the bit I have right now, which, I know is pitifully small, I'm just blanking out today it seems.

  function loadUrlData()
  {
    // hold the raw data, /blah/blah/
    $urlData = array();

    $res = sql::query( "SELECT DISTINCT(`url`) FROM `pages` ORDER BY `url` ASC" );
    while( $row = sql::getarray( $res ) )
    {
      $urlData[] = explode( '/', substr( $row['url'], 1, -1 ) );
    }

    // populated, eventually, with the parent > child data
    $treeData = array();

    // a url
    foreach( $urlData as $k=> $v )
    {
      // the url pieces
      foreach( $v as $k2=> $v2 )
      {

      }
    }

    // $treeData eventually
    return $urlData;
  }


推荐答案

看起来很简单。您想遍历所有行( foreach ),将它们拆分成几个部分( explode ),遍历它们( foreach )并对其进行分类。

Looks rather easy. You want to loop through all lines (foreach), split them into parts (explode), loop through them (foreach) and categorize them.

由于您不喜欢请求大量代码,因此我不会提供任何内容。

Since you don't like asking for a chunk of code, I won't provide any.

更新

一种很好的解决方法是引用 $ urlTree (使用& ),遍历URL的每个部分,并不断更新变量,例如 $ currentPosition 到URL树中的当前部分。因为您使用& ,所以您可以直接使用简单变量直接编辑数组。

Update
A very nice way to solve this is to reference the $urlTree (use &), loop through every part of the URL and keep updating a variable like $currentPosition to the current part in the URL tree. Because you use &, you can simply edit the array directly while still using a simple variable.

更新2

这可能有效:

Update 2
This might work:

// a url
foreach( $urlData as $k=> $v )
{
  $currentSection = &$treeData;
  // the url pieces
  foreach( $v as $k2=> $v2 )
  {
    if (!isset($currentSection[$v2])) {
      $currentSection[$v2] = array();
    }

    $currentSection = &$currentSection[$v2];
  }
}

这篇关于PHP-创建层次结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆