如何使用foreach循环回显多维数组

时间:2021-10-08 19:45:07

I have two variables that contain some data as shown below

我有两个包含一些数据的变量,如下所示

$main_headings = [Business Structure,Business Management];

to make comma separated array i use explode

使用逗号分隔数组我使用爆炸

$main_headings = explode [',','Business Structure,Business Management'];

and second variable contain data like this

第二个变量包含这样的数据

$sub_headins = [Competitive Positioning, Diversification of Fund Mix, Unit Holding Pattern-Management Quality, Organizational Structure];

same i use explode with second variable to make array

同样我使用第二个变量爆炸来制作数组

$sub_headins = explode ('-', [Competitive Positioning, Diversification of Fund Mix, Unit Holding Pattern-Management Quality, Organizational Structure];)

After this my second variable convert into two arrays

在此之后我的第二个变量转换为两个数组

array1() = [Competitive Positioning, Diversification of Fund Mix, Unit Holding Pattern];

array2() = [Management Quality, Organizational Structure];

now i want my output like this

现在我想要这样的输出

  • Business Structure

    • Competitive Positioning

    • Diversification of Fund Mix

      基金组合的多元化

    • Unit Holding Pattern

      单位控股模式

  • Business Management

    • Management Quality
    • Organizational Structure
  • 企业管理质量组织结构

I use for each but i failed to get this output

我使用的每一个,但我没有得到这个输出

here is my code

这是我的代码

foreach ($main_headings as $main_heading) {
    echo $main_heading;
    echo '<br>';
    foreach ($sub_headings as $sub_heading) {
        echo $sub_heading;
         echo '<br>';
    }
}

output of my code

输出我的代码

Business Structure Competitive Positioning, Diversification of Fund Mix, Unit Holding Pattern Management Quality, Organizational Structure Business Management Competitive Positioning, Diversification of Fund Mix, Unit Holding Pattern Management Quality, Organizational Structure

企业结构竞争定位,基金组合多元化,单位控股模式管理质量,组织结构企业管理竞争定位,基金组合多元化,单位控股模式管理质量,组织结构

2 个解决方案

#1


1  

echo "<ul>";
foreach ($main_headings as $main_heading) {
    echo "<li>";
    echo $main_heading;
    echo "<ul>";
        foreach ($sub_headings as $sub_heading) {
            echo "<li>";
            echo $sub_heading;
            echo '</li>';
        }
    echo "</ul>";
    echo "</li>";
}
echo "</ul>";

#2


1  

try this:

foreach ($main_headings as $main_heading) {
    echo '<p>' . $main_heading . '</p>';
    foreach ($sub_headings as $sub_heading) {
        echo '<p style="text-indent=10px;">' . $sub_heading . '</p>';
    }
}

#1


1  

echo "<ul>";
foreach ($main_headings as $main_heading) {
    echo "<li>";
    echo $main_heading;
    echo "<ul>";
        foreach ($sub_headings as $sub_heading) {
            echo "<li>";
            echo $sub_heading;
            echo '</li>';
        }
    echo "</ul>";
    echo "</li>";
}
echo "</ul>";

#2


1  

try this:

foreach ($main_headings as $main_heading) {
    echo '<p>' . $main_heading . '</p>';
    foreach ($sub_headings as $sub_heading) {
        echo '<p style="text-indent=10px;">' . $sub_heading . '</p>';
    }
}