分组数组中的PHP数组分组

时间:2014-11-21 18:52:09

标签: php mysql arrays codeigniter

我已经将我的主数组分组,因此我可以显示DNS记录类型的选项卡格式。 在每个选项卡中显示所有类型

中存在的所有DNS记录

例如:

[22] => Array
    (
        [dnsType] => MicrosoftDNS_AType
        [dnsDomain] => 1fit.net
        [dnsOwner] => 1fit.net
        [dnsIP] => 127.0.0.1
        [dnsText] => 1fit.net IN A 127.0.0.1
        [dnsTTL] => 3600
    )

[23] => Array
    (
        [dnsType] => MicrosoftDNS_AType
        [dnsDomain] => 1fit.net
        [dnsOwner] => mail.1fit.net
        [dnsIP] => 127.0.0.1
        [dnsText] => mail.1fit.net IN A 127.0.0.1
        [dnsTTL] => 3600
    )

[24] => Array
    (
        [dnsType] => MicrosoftDNS_AType
        [dnsDomain] => 1fit.net
        [dnsOwner] => www.1fit.net
        [dnsIP] => 127.0.0.1
        [dnsText] => www.1fit.net IN A 127.0.0.1
        [dnsTTL] => 3600
    )

如何通过对dnsDomain进行分组来进一步细分?这样,上面的所有3个记录都出现在表格

中的主“域”下

现在,我似乎只能将它们全部显示出来:http://prntscr.com/58oov5

我想要的是这3个浓缩成1行,有3个记录的ul ...至少我可以制作是可折叠的,所以只有第1个记录会显示为链接...

这是我迄今为止的尝试:

<div id="tab-content" class="tab-content">
<?php
    $gCt = count($groups);
    for($i=0;$i<$gCt;++$i){
        $cls = ($i == 0) ? ' active' : '';
        echo '<div class="tab-pane'.$cls.'" id="' . strtolower(str_replace('Type', null, str_replace('MicrosoftDNS_', null, $groups[$i]))) . '">
            <div class="content-container">
                <h3>' . str_replace('Type', null, str_replace('MicrosoftDNS_', null, $groups[$i])) . ' Records</h3>';
        echo '<div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Domain</th>
                        <th>IP</th>
                        <th>TTL</th>        
                    </tr>       
                </thead>';
            $recs = array_values(filter_by_value($dns, 'dnsType', $groups[$i]));        
            $rCt = count($recs);
        echo '<textarea style="width:100%;height:200px;">';
        print_r($recs);
        echo '</textarea>';
            for($j = 0; $j < $rCt; ++$j){
                $domains = array_values(filter_by_value($recs, 'dnsDomain', $recs[$j]['dnsDomain']));
                $dCt = count($domains);
                echo '<tr>
                        <td><a href="#" onclick="return false;" title="TEXT: ' . $recs[$j]['dnsText'] . '">' . $recs[$j]['dnsOwner'] . '</a></td>
                        <td>' . $recs[$j]['dnsIP'] . '</td>
                        <td>' . $recs[$j]['dnsTTL'] . '</td>
                    </tr>';
            }
        echo '
            </table>
        </div>';
        echo '</div>
            </div>';

    }
?>
</div>
<?php
function filter_by_value ($array, $index, $value){ 
if(is_array($array) && count($array)>0)  
{ 
    foreach(array_keys($array) as $key){ 
        $temp[$key] = $array[$key][$index]; 

        if ($temp[$key] == $value){ 
            $newarray[$key] = $array[$key]; 
        } 
    } 
  } 
return $newarray; 
} 
?>

引入$groups$dns的函数(它的代码编号)

function dns(){
    $data['dns'] = $this->servers_model->get_DNS();
    $tmpGrp = $this->array_group_by($data['dns'], 'dnsType');
    $_tmparr = array();
    $i = 0;
    foreach($tmpGrp as $key => $value) {
        $_tmparr[$i] = $key;
        ++$i;
    }       
    $data['groups'] = $_tmparr;
    $this->load->view('templates/header', $data);
    $this->load->view('servers/dns', $data);
    $this->load->view('templates/footer', $data);
}

function array_group_by($array, $key) {
    $return = array();
    foreach($array as $val) {
        $return[$val[$key]][] = $val;
    }
    return $return;
}

0 个答案:

没有答案