如何将一维数组转换为嵌套的<li>?

时间:2019-07-08 20:41:39

标签: php html arrays

如何将一维数组转换为多维数组或嵌套的<li> HTML。

下面是我需要基于<li>的嵌套parent_id中的数组

Array (
[5] => Array
    (
        [data_id] => 5
        [data_parent] => 4
        [data_level] => 
        [data_prefix] => 
        [data_title] => Assets
        [data_link] => 0
    )

[57] => Array
    (
        [data_id] => 57
        [data_parent] => 5
        [data_level] => 
        [data_prefix] => 
        [data_title] => Fixed Assets
        [data_link] => 0
    )

[52] => Array
    (
        [data_id] => 52
        [data_parent] => 5
        [data_level] => 
        [data_prefix] => 
        [data_title] => Asset Two
        [data_link] => 1
    )

[51] => Array
    (
        [data_id] => 51
        [data_parent] => 5
        [data_level] => 
        [data_prefix] => 
        [data_title] => Assset ONE
        [data_link] => 1
    )

[48] => Array
    (
        [data_id] => 48
        [data_parent] => 4
        [data_level] => 
        [data_prefix] => 
        [data_title] => Expenses
        [data_link] => 0
    )

[50] => Array
    (
        [data_id] => 50
        [data_parent] => 48
        [data_level] => 
        [data_prefix] => 
        [data_title] => Expense One
        [data_link] => 1
    )

[49] => Array
    (
        [data_id] => 49
        [data_parent] => 48
        [data_level] => 
        [data_prefix] => 
        [data_title] => Expense One
        [data_link] => 1
    )

[58] => Array
    (
        [data_id] => 58
        [data_parent] => 57
        [data_level] => 
        [data_prefix] => 
        [data_title] => Vehicles
        [data_link] => 1
    )

)

2 个答案:

答案 0 :(得分:0)

您可以使用foreach和sort创建多维数组

$newArray = [];

foreach($array as $value) {
    $newArray[$value['data_parent']][] = $value;
}

var_dump($newArray);

答案 1 :(得分:0)

您可以先创建父子递归数组,

function buildTree(array $elements, $options = [
    'parent_id_column_name' => 'parent_id',
    'children_key_name'     => 'children',
    'id_column_name'        => 'id',
], $parentId = 0) {
    $branch = [];
    foreach ($elements as $element) {
        if ($element[$options['parent_id_column_name']] == $parentId) {
            $children = buildTree($elements, $options, $element[$options['id_column_name']]);
            if ($children) {
                $element[$options['children_key_name']] = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}
// 4 as first parent is
$temp = buildTree($arr, [
    'parent_id_column_name' => 'data_parent',
    'children_key_name'     => 'children',
    'id_column_name'        => 'data_id'], 4);

然后将ul和li递归应用于上述数组,

// 4 as first parent is
$temp = buildTree($arr, [
    'parent_id_column_name' => 'data_parent',
    'children_key_name'     => 'children',
    'id_column_name'        => 'data_id'], 4);
function printTree($tree)
{
    if (!is_null($tree) && count($tree) > 0) {
        echo '<ul>';
        foreach ($tree as $node) {
            // you can use whole $node here including all values of each node
            echo '<li>' . $node['data_title'];
            if (!empty($node['children'])) {
                printTree($node['children']);
            }
            echo '</li>';
        }
        echo '</ul>';
    }
}
printTree($temp);

Demo

输出:-

<ul>
    <li>Assets<ul>
            <li>Fixed Assets<ul>
                    <li>Vehicles</li>
                </ul>
            </li>
            <li>Asset Two</li>
            <li>Assset ONE</li>
        </ul>
    </li>
    <li>Expenses<ul>
            <li>Expense One</li>
            <li>Expense One</li>
        </ul>
    </li>
</ul>

我参考了答案link1link2