dockerme
dockerme

Reputation: 11

I'm Trying to format the output of an array using php

I'm Trying to format the output of an array using php but I can't seem to get the keys and values on the same line. I've listed the code that I'm using to display the keys and values but this code outputs the keys and values on different lines

function olLiTree($tree)
{
    echo '<pre>';
    foreach($tree as $key => $item) {
        if (is_array($item)) {
            echo '<pre>', $key ;
            olLiTree($item);
            echo '</pre>';
        } else {
            echo '<pre>', $item, '</pre>'; 
        }
    }
    echo '</ul>';
}

print(olLiTree($results));

Upvotes: 1

Views: 49

Answers (2)

RJParikh
RJParikh

Reputation: 4166

Use <ul> <li> .... </li></ul>. Also remove comma(,) because PHP use dot(.) for concat string.

function olLiTree($tree)
{
    echo '<ul>';
    foreach($tree as $key => $item) {
        if (is_array($item)) {
            echo '<li>'. $key ;
            olLiTree($item);
            echo '</li>';
        } else {
            echo '<li>' .$item. '</li>'; 
        }
    }
    echo '</ul>';
}

print(olLiTree($results));

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

You use comma , instead of dot . for string concatenation php use dot

    function olLiTree($tree)
{
    echo '<pre>';
    foreach($tree as $key => $item) {
        if (is_array($item)) {
            echo '<pre>'. $key ;
            olLiTree($item);
            echo '</pre>';
        } else {
            echo '<pre>' . $item. '</pre>'; 
        }
    }
    echo '</ul>';
}

print(olLiTree($results));

Upvotes: 0

Related Questions