简体   繁体   中英

How to echo an multidimensional array using foreach loop

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

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>";

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>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM