m1e*_*1b1 7 php html-lists multidimensional-array
我有以下数组:
$ tree_array
当我做var_dump时,我得到:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是使用此数据创建无序列表和列表(ul,li).随着数据库的更新,将会有更多数据添加到数组中,因此它将继续增长.我的目标是遍历数组并让它创建以下代码,并能够随着数据的增长继续创建列表.我是php新手,不知道如何实现这一目标.
<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...
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!先感谢您!
对我来说似乎是一个足够简单的递归:
function arrayToList($in) {
echo "<ul>";
foreach($in as $v) {
if( is_array($v)) arrayToList($v);
else echo '<li>' . $v . '</li>';
}
echo "</ul>";
}
Run Code Online (Sandbox Code Playgroud)
看起来您那里有一些重复的值。你正在用mysql_fetch_array吗 ?您应该使用mysql_fetch_assoc或mysql_fetch_row取决于您是否需要关联数组或索引数组。