1

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

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.