将PHP数组输出到无序列表?

时间:2019-11-18 16:39:14

标签: php arrays

这显然是一个简单的项目,这里还有关于该任务的几个问题,但是尝试这些解决方案时会导致错误。

在WordPress中,我有一个数组,在打印时显示以下内容:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 5
            [name] => Dog
            [slug] => dog
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => wpsl_store_category
            [description] => 
            [parent] => 0
            [count] => 4
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 7
            [name] => Cat
            [slug] => cat
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => wpsl_store_category
            [description] => 
            [parent] => 0
            [count] => 4
            [filter] => raw
        )

    [2] => WP_Term Object
        (
            [term_id] => 4
            [name] => Horse
            [slug] => horse
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => wpsl_store_category
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

尝试将数组回显为每个[名称]的无序列表。

因此,当执行<?php echo $myArray; ?>时,它应该输出

<ul>
<li>Dog</li>
<li>Cat</li>
<li>Horse</li>
</ul>

2 个答案:

答案 0 :(得分:2)

似乎您的数组包含一些术语对象。假设数组为$term_objs,那么您可以使用以下代码来打印术语名称,

foreach ($term_objs as $term_obj ):
    echo '<li>'.$term_obj->name.'</li>';
endforeach;

答案 1 :(得分:2)

为我工作

<?php
$ul = '<ul>';
foreach ($abc as $key => $value) {
    $ul.= '<li>'.$value->item.'</li>';
}
$ul .= '</ul>';
?>