简体   繁体   English

我正在尝试使用 php 格式化数组的输出

[英]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.我正在尝试使用 php 格式化数组的输出,但我似乎无法在同一行上获取键和值。 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));

Use <ul> <li> .... </li></ul> .使用<ul> <li> .... </li></ul> Also remove comma(,) because PHP use dot(.) for concat string.还要删除comma(,)因为PHP 使用dot(.)作为连接字符串。

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

You use comma , instead of dot .您使用逗号,而不是点. for string concatenation php use dot对于字符串连接 php 使用点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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