PHP 遍历并打印多维数组

发布于 2024-12-07 07:35:32 字数 925 浏览 0 评论 0原文

任何人都可以帮助我在这里编码吗?

我得到了以下数组配置:

$array[1]['areaname'] = 'Area 1';
$array[1][1]['areaname'] = 'Sub Area 1';
$array[1][2]['areaname'] = 'Sub Area 2';
$array[1][3]['areaname'] = 'Sub Area 3';
$array[2]['areaname'] = 'Area 2';
$array[2][1]['areaname'] = 'Sub Area 1';

我想显示以下内容:

<ul>
    <li>
        Area 1
        <ul>
            <li>Sub Area 1</li>
            <li>Sub Area 2</li>
            <li>Sub Area 3</li>
        </ul>
    </li>
    <li>
        Area 2
        <ul>
            <li>Sub Area 1</li>
        </ul>
    </li>
</ul>

我需要一个代码,可以在其中拥有所需数量的子区域。例:

$array[1][1][2][3][4]['areaname'];

还有一个条件。该数组包含其他元素,例如 $array[1]['config']、$array[1][2][3]['link'] 或 $array[1][另一个不应放入的元素数组循环] ...我只需要打印区域名称。

anyone can help me with some coding here?

I got the following array config:

$array[1]['areaname'] = 'Area 1';
$array[1][1]['areaname'] = 'Sub Area 1';
$array[1][2]['areaname'] = 'Sub Area 2';
$array[1][3]['areaname'] = 'Sub Area 3';
$array[2]['areaname'] = 'Area 2';
$array[2][1]['areaname'] = 'Sub Area 1';

I want display the following:

<ul>
    <li>
        Area 1
        <ul>
            <li>Sub Area 1</li>
            <li>Sub Area 2</li>
            <li>Sub Area 3</li>
        </ul>
    </li>
    <li>
        Area 2
        <ul>
            <li>Sub Area 1</li>
        </ul>
    </li>
</ul>

I need a code where I can have as many sub area as I want. Example:

$array[1][1][2][3][4]['areaname'];

There are also another condition. The array got other elements such as $array[1]['config'], $array[1][2][3]['link'] or $array[1][another array of elements that should not be into the loop] ... I only need print the areaname.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-12-14 07:35:33
$array = array();
$array[1]['areaname'] = 'Area 1';
$array[1][1]['areaname'] = 'Sub Area 1';
$array[1][2]['areaname'] = 'Sub Area 2';
$array[1][3]['areaname'] = 'Sub Area 3';
$array[2]['areaname'] = 'Area 2';
$array[2][1]['areaname'] = 'Sub Area 1';

function generate_html_list_recursive( &$data, $labelKey )
{
    // begin with an empty html string
    $html = '';

    // loop through all items in this level
    foreach( $data as $key => &$value )
    {
        // where only interested in numeric items
        // as those are the actual children
        if( !is_numeric( $key ) )
        {
            // otherwise continue
            continue;
        }

        // if no <li> has been created yet, open the <ul>
        $html .= empty( $html ) ? '<ul>' : '';

        // extract the label from this level's array, designated by $labelKey
        $label = isset( $value[ $labelKey ] ) ? $value[ $labelKey ] : '';

        // open an <li> and append the label
        $html .= '<li>' . $label;

        // call this funcion recursively
        // with the next level ($value) and label key ($labelKey)
        // it will figure out again whether that level has numeric children as well
        // returns a new complete <ul>, if applicable, otherwise an empty string
        $html .= generate_html_list_recursive( $value, $labelKey );

        // close our currently open <li>
        $html .= '</li>';
    }

    // if this level has <li>'s, and therefor an opening <ul>, close the <ul>
    $html .= !empty( $html ) ? '</ul>' : '';

    // return the resulting html
    return $html;
}

echo generate_html_list_recursive( $array, 'areaname' );
$array = array();
$array[1]['areaname'] = 'Area 1';
$array[1][1]['areaname'] = 'Sub Area 1';
$array[1][2]['areaname'] = 'Sub Area 2';
$array[1][3]['areaname'] = 'Sub Area 3';
$array[2]['areaname'] = 'Area 2';
$array[2][1]['areaname'] = 'Sub Area 1';

function generate_html_list_recursive( &$data, $labelKey )
{
    // begin with an empty html string
    $html = '';

    // loop through all items in this level
    foreach( $data as $key => &$value )
    {
        // where only interested in numeric items
        // as those are the actual children
        if( !is_numeric( $key ) )
        {
            // otherwise continue
            continue;
        }

        // if no <li> has been created yet, open the <ul>
        $html .= empty( $html ) ? '<ul>' : '';

        // extract the label from this level's array, designated by $labelKey
        $label = isset( $value[ $labelKey ] ) ? $value[ $labelKey ] : '';

        // open an <li> and append the label
        $html .= '<li>' . $label;

        // call this funcion recursively
        // with the next level ($value) and label key ($labelKey)
        // it will figure out again whether that level has numeric children as well
        // returns a new complete <ul>, if applicable, otherwise an empty string
        $html .= generate_html_list_recursive( $value, $labelKey );

        // close our currently open <li>
        $html .= '</li>';
    }

    // if this level has <li>'s, and therefor an opening <ul>, close the <ul>
    $html .= !empty( $html ) ? '</ul>' : '';

    // return the resulting html
    return $html;
}

echo generate_html_list_recursive( $array, 'areaname' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文