dongzhansong5785 2015-08-30 13:51
浏览 25
已采纳

php数组计数父/子

I can't get it working right. What i want is that counter will count every parent and inside parent child that will look like table of content, result needs to be:

<ul>
  <li><span style="color: red;">1</span>  <strong>a</strong>
  </li>
  <ul>
    <li><span style="color: red;">1.1</span>  <strong>b</strong>
    </li>
    <li><span style="color: red;">1.2</span>  <strong>c</strong>
    </li>
  </ul>
  <li><span style="color: red;">2</span>  <strong>e</strong>
  </li>
  <ul>
    <li><span style="color: red;">2.1</span>  <strong>f</strong>
    </li>
    <li><span style="color: red;">2.2</span>  <strong>g</strong>
    </li>
  </ul>
  <li><span style="color: red;">3</span>  <strong>h</strong>
  </li>
  <ul>
    <li><span style="color: red;">3.1</span>  <strong>k</strong>
    </li>
    <li><span style="color: red;">3.2</span>  <strong>l</strong>
    </li>
    <ul>
      <li><span style="color: red;">3.2.1</span>  <strong>m</strong>
      </li>
      <li><span style="color: red;">3.2.2</span>  <strong>n</strong>
      </li>
    </ul>
  </ul>
</ul>

There are n childs soo deep is unlimited.. My current code is here:

<?php 
$arr = array(
    array('id' => 1, 'title' => 'a', 'parent' => 0),
    array('id' => 2, 'title' => 'b', 'parent' => 1),
    array('id' => 3, 'title' => 'c', 'parent' => 1), 
    array('id' => 5, 'title' => 'e', 'parent' => 0),
    array('id' => 6, 'title' => 'f', 'parent' => 5),
    array('id' => 7, 'title' => 'g', 'parent' => 5), 
    array('id' => 8, 'title' => 'h', 'parent' => 0),
    array('id' => 9, 'title' => 'k', 'parent' => 8),
    array('id' => 10, 'title' => 'l', 'parent' => 8), 
    array('id' => 11, 'title' => 'm', 'parent' => 10), 
    array('id' => 12, 'title' => 'n', 'parent' => 10), 
);

function checkChilds($arr, $id){
    foreach ($arr as $key => $value) {
        if ($value['parent'] == $id) {
            return true;
        }
    }
    return false;
}

function getList($arr, $parent = 0, $subcounter = 1){
    $output = null;

    $output .= '<ul>';
    $counterParent = 1;
    $countChild = 1;
    foreach($arr as $key => $value) {
        if($value['parent'] == $parent){

            $counter = ($value['parent'] == 0) ? "{$counterParent}" : "{$subcounter}.{$countChild}";
            $output .= "<li><span style='color: red;'>{$counter}</span> <strong>{$value['title']}</strong></li>";

            if (checkChilds($arr, $value['id'])){
                $output .= getList($arr, $value['id'], $counterParent);
                $countChild++;
            }

            // counter for parent
            if($value['parent'] == 0) {
                $counterParent++;
            }
        }
    }
    $output .= '</ul>';

    return $output;
}

echo getList($arr);
</div>
  • 写回答

2条回答 默认 最新

  • douhuanchi6586 2015-08-30 14:35
    关注

    Try this:

    <?php 
    $arr = array(
        array('id' => 1, 'title' => 'a', 'parent' => 0),
        array('id' => 2, 'title' => 'b', 'parent' => 1),
        array('id' => 3, 'title' => 'c', 'parent' => 1), 
        array('id' => 5, 'title' => 'e', 'parent' => 0),
        array('id' => 6, 'title' => 'f', 'parent' => 5),
        array('id' => 7, 'title' => 'g', 'parent' => 5), 
        array('id' => 8, 'title' => 'h', 'parent' => 0),
        array('id' => 9, 'title' => 'k', 'parent' => 8),
        array('id' => 10, 'title' => 'l', 'parent' => 8), 
        array('id' => 11, 'title' => 'm', 'parent' => 10), 
        array('id' => 12, 'title' => 'n', 'parent' => 10), 
    );
    
    function checkChilds($arr, $id){
        foreach ($arr as $key => $value) {
            if ($value['parent'] == $id) {
                return true;
            }
        }
        return false;
    }
    
    function getList($arr, $parent = 0, $subcounter = 1){
        $output = null;
    
        $output .= '<ul>';
        $counterParent = 1;
        $countChild = 1;
        foreach($arr as $key => $value) {
            if($value['parent'] == $parent){
    
                $counter = ($value['parent'] == 0) ? "{$counterParent}" : "{$subcounter}.{$countChild}";
                $output .= "<li><span style='color: red;'>{$counter}</span> <strong>{$value['title']}</strong></li>";
    
                if (checkChilds($arr, $value['id'])){
                    $output .= getList($arr, $value['id'], $counter);
                }
                $countChild++;
                // counter for parent
                if($value['parent'] == 0) {
                    $counterParent++;
                }
            }
        }
        $output .= '</ul>';
    
        return $output;
    }
    
    echo getList($arr);
    

    You didn't pass the getList function all the prefix numbers, only the immediate parent. And you have to increase the counter always, not only if a new getList call is necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度