This is my array

Array
(
    [33] => Array
        (
            [for sale] => Array
                (
                    [1] => villas
                    [2] => plots
                    [3] => flats
                    [4] => houses
                )

        )

    [34] => Array
        (
            [for rent] => Array
                (
                    [5] => in house
                    [6] => appartment
                    [7] => godams
                )

        )

    [35] => Array
        (
            [hostels] => Array
                (
                    [8] => boys
                    [9] => girls
                )

        )

    [36] => Array
        (
            [pg accommodation] => Array
                (
                    [10] => boys
                    [11] => girls
                )

        )

)

and my php code is this..

while($row = mysql_fetch_array($select4)){
    $final[$row['child_category_id']][$row['child_category_name']][$row['sub_child1_category_id']] = $row['sub_child1_category_name'];

}
echo '<pre>';print_r($final);echo'</pre>';



foreach($final as $k1 => $arr1)
{

    foreach($arr1 as $k2 => $arr2){
           echo '<b>'.$k2.'</b><br>';
        foreach($arr2 as $subcat){
            echo $subcat.'<br/>';
        }

    }
}

It displays the records like this

for sale
villas
plots
flats
houses

for rent
in house
appartment
godams

hostels
boys
girls

pg accommodation
boys
girls

How to align them inline..
i need to display 2 records in a row

eg:

For sale and For rent with sub categories in 1 row

Then

hostels and pg accomodation in another row

Dani AI

Generated

A simple, robust way to get two category blocks per row is to output each category as a semantic block (title + a UL of subitems) and let CSS handle the two-up layout. 's implode suggestion is useful if you want a single comma-separated line of subcats, but it doesn't control layout across columns. Flatten your nested array to a list of "blocks", chunk that list into pairs, then render each pair inside a wrapper so CSS can place two blocks per row. Escape output (htmlspecialchars) and, if running on recent PHP, avoid old mysql_* calls—use mysqli or PDO.

Example PHP pattern (different from the code already in the thread — this flattens and chunks into pairs):

<?php
$blocks = [];
foreach ($final as $group) {
    foreach ($group as $title => $items) {
        $blocks[] = ['title' => $title, 'items' => $items];
    }
}

foreach (array_chunk($blocks, 2) as $pair) {
    echo '<div class="cat-row">';
    foreach ($pair as $b) {
        echo '<div class="cat">';
        echo '<h4>' . htmlspecialchars($b['title']) . '</h4>';
        echo '<ul>';
        foreach ($b['items'] as $it) {
            echo '<li>' . htmlspecialchars($it) . '</li>';
        }
        echo '</ul></div>';
    }
    echo '</div>';
}
?>

CSS that makes those pairs layout cleanly (Grid with a simple responsive fallback):

.cat-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.cat { border: 1px solid #e5e5e5; padding: 8px; box-sizing: border-box; }
@media (max-width: 640px) { .cat-row { grid-template-columns: 1fr; } }

Troubleshooting notes: if a theme or other rules interfere, increase selector specificity or reset margins. For consistent heights when sublists differ, use align-items: start on a flex fallback or let Grid stretch items. If you only want inline comma lists (no block layout), 's implode approach is perfectly fine inside the markup above.

Member Avatar for Member #120589

You can use the implode() function to get a list of subcats:

foreach($final as $k1 => $arr1)
{
    foreach($arr1 as $k2 => $arr2){
           echo '<p><strong>'.$k2.'</strong></p>';
           echo "<p>" . implode(", ", $arr2) . "</p>";
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.