从multidimentional php数组构建父子类别breadcrumb

时间:2018-05-14 21:06:43

标签: php multidimensional-array codeigniter-3

帮我构建一个动态函数来显示任意数量的子类别。截至目前,只有三个级别的类别。请指导我构建一个代码来处理单个父项的任意数量的子类别,并将其显示为面包屑格式。

下面是codeigniter索引函数,从类别表中获取所有类别

public function index() {
    $dbdata['table'] = 'categories';
    $dbdata['select'] = array('id', 'parent', 'name');
    $dbdata['where'] = array('type' => 'post');
    $records = $this->ot->get_records($dbdata);
    $category = array();
    foreach ($records as $record) {
        if ($record['parent']) {
            $parent = $this->get_parent($record['parent'], $records);
            if ($parent['parent']) {
                $super_parent = $this->get_parent($parent, $records);
                $category[] = $super_parent['name'] . ' > ' . $parent['name'] . ' > ' . $record['name'];
            } else {
                $category[] = $parent['name'] . ' > ' . $record['name'];
            }
        } else {
            $category[] = $record['name'];
        }
    }
    echo '
<pre>';
    print_r($records);
    echo '<hr>';
    print_r($category);
    echo '</pre>';
}

protected function get_parent($parent, $records) {
    $key = array_search($parent, array_column($records, 'id'));
    return $records[$key];
}

获得输出:

Array
(
    [0] => Array
    (
        [id] => 1
        [parent] => 0
        [name] => Category 1
    )
    [1] => Array
    (
        [id] => 2
        [parent] => 1
        [name] => Category 2
    )
    [2] => Array
    (
        [id] => 3
        [parent] => 2
        [name] => Category 3
    )
    [3] => Array
    (
        [id] => 4
        [parent] => 0
        [name] => Category 4
    )
    [4] => Array
    (
        [id] => 5
        [parent] => 4
        [name] => Category 5
    )
    [5] => Array
    (
        [id] => 6
        [parent] => 0
        [name] => Category 6
    )
    [6] => Array
    (
        [id] => 7
        [parent] => 0
        [name] => Category 7
    )
    [7] => Array
    (
        [id] => 8
        [parent] => 0
        [name] => Category 8
    )
    [8] => Array
    (
        [id] => 9
        [parent] => 0
        [name] => Category 9
    )
    [9] => Array
    (
        [id] => 10
        [parent] => 0
        [name] => Category 10
    )
    [10] => Array
    (
        [id] => 11
        [parent] => 0
        [name] => Category 11
    )
    [11] => Array
    (
        [id] => 12
        [parent] => 0
        [name] => Category 12
    )
)
Array
(
    [0] => Category 1
    [1] => Category 1 > Category 2
    [2] => Category 1 > Category 2 > Category 3
    [3] => Category 4
    [4] => Category 4 > Category 5
    [5] => Category 6
    [6] => Category 7
    [7] => Category 8
    [8] => Category 9
    [9] => Category 10
    [10] => Category 11
    [11] => Category 12
)

1 个答案:

答案 0 :(得分:0)

试试此代码

string targetSiteURL = @"https://xx.sharepoint.com/sites/lz";

var login = "lz@xxx.onmicrosoft.com";
var password = "xxx";

var securePassword = new SecureString();

foreach (char c in password)
{
    securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
var web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();

var filePath = web.ServerRelativeUrl + "/Shared%20Documents/09.%20SharePoint%20Tutorials/SharePoint%20365%20Co-authoring%20excel%20files.docx";
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, filePath);

using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
    String line = sr.ReadToEnd();
    Console.WriteLine(line);
}
Console.ReadKey();