使用php中的多维数组创建ul和li [英] create ul and li using a multidimensional array in php

查看:195
本文介绍了使用php中的多维数组创建ul和li的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

$ tree_array

$tree_array

当我执行var_dump时,我得到:

When I do a var_dump, I get:

array(6) {
    [0]=> string(23) "$100,000 Cash Flow 2013"
    [1]=> array(6) {
        [0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
        [1]=> string(13) "Sell Iron Oak" ["Opportunity"]=> string(13) "Sell Iron Oak"
        [2]=> string(2) "10" ["OID"]=> string(2) "10"
    }
    [2]=> array(2) {
        [0]=> string(32) "ask her if she would like to buy" ["Activity"]=> string(32) "ask her if she would like to buy"
    }
    [3]=> array(6) {
        [0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
        [1]=> string(8) "Sell Car" ["Opportunity"]=> string(8) "Sell Car"
        [2]=> string(2) "11" ["OID"]=> string(2) "11"
    }
    [4]=> array(2) {
            [0]=> string(52) "Call Roy back to see if he would like to purchase it" ["Activity"]=> string(52) "Call Roy back to see if he would like to purchase it"
    }
    [5]=> array(1) {
        ["tot_opp"]=> NULL
    }
} 

我的最终目标是使用这些数据创建无序列表和列表(ul,li).随着数据库的更新,将有更多数据添加到阵列中,因此它将保持增长.我的目标是遍历数组,并使其创建以下代码,并能够随着数据的增长而继续创建列表.我是php的新手,不确定如何做到这一点.

My end goal is to create unordered lists and lists (ul, li) with this data. There will be more data added to the array as the database gets updated, so it will keep growing. My goal is to loop through the array and have it create the following code and be able to keep creating lists as the data grows. I am new to php and not sure how to accomplish this.

<ul>
<li>$100,000 Cash Flow 2013</li>
<ul>
<li>Sell Iron Oak</li>
<ul>
<li>ask her if she would like to buy</li>
</ul>
<ul>
<li>Sell Car</li>
</ul>etc...

任何帮助将不胜感激!预先谢谢你!

Any help will be greatly appreciated! Thank you in advance!

推荐答案

对我来说似乎很简单的递归:

Seems like a simple enough recursion to me:

function arrayToList($in) {
  echo "<ul>";
  foreach($in as $v) {
    if( is_array($v)) arrayToList($v);
    else echo '<li>' . $v . '</li>';
  }
  echo "</ul>";
}

似乎您那里有一些重复的值.您正在使用mysql_fetch_array吗?您应该使用mysql_fetch_assoc还是mysql_fetch_row,这取决于您需要关联数组还是索引数组.

It looks like you have some duplicate values up there. Are you using mysql_fetch_array? You should be using mysql_fetch_assoc or mysql_fetch_row depending on whether you need an associative or indexed array.

这篇关于使用php中的多维数组创建ul和li的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆