php数组计数父/子

时间:2015-08-30 13:51:47

标签: php arrays counter

我无法正常工作。我想要的是,计数器将计算每个父母和内部父母孩子看起来像内容表,结果需要是:



<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>
&#13;
&#13;
&#13;

有n个孩子太深,是无限的.. 我目前的代码在这里:

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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

您没有将getList函数传递给所有前缀号,只传递了直接父级。并且您必须始终增加计数器,不仅是需要新的getList调用。

答案 1 :(得分:0)

完整,简化的解决方案:     

$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 getList(&$arr, $level, $depth = '') {
    $count = 1;
    $ret = '<ul>';
    foreach($arr as $val) {
        if($val['parent'] === $level) {
            $ret .= '<li><span style="color: red;">' . ($depth !== '' ? $depth . '.' . $count : $count) . '</span> <strong>' . htmlentities($val['title'], ENT_COMPAT, 'UTF-8') . '</strong>';
            $ret .= getList($arr, $val['id'], $depth !== '' ? $depth . '.' . $count : $count);
            $ret .= '</li>';
            $count ++;
        }
    }
    $ret .= '</ul>';

    if($ret === '<ul></ul>') {
        return '';
    } else {
        return $ret;
    }
}

echo getList($arr, 0);

?>

请记住,对于较大的数据集,这将非常慢。

我假设$arr基于某种数据库驱动的数据集;有关关系数据库中的分层数据,请参阅this article