如何在多维数组中按2个值对结果进行分组

时间:2016-07-12 05:06:52

标签: php arrays multidimensional-array

[.NET Frameworks] => Array (
    [WPF] => Array (
        [user1] => No Knowledge
        [user2] => No Knowledge
    )
[) Java Frameworks] => Array (
    [Hibernate] => Array (
        [user1] => No Knowledge
        [user2] => Solid Understanding
        )
    )
[PHP Frameworks] => Array (
    [CakePHP] => Array (
        [user1] => Some Knowledge
        )
    [CodeIgniter] => Array (
        [user1] => Solid Understanding
        [user3] => Solid Understanding
        [user2] => No Knowledge
        [user4] => No Knowledge
        )
    [Pear] => Array (
        [user1] => Some Knowledge
        )
    )
[Web] => Array (
    [HTML] => Array (
        [user1] => Solid Understanding
        )
    )

上下文
每个技能组(例如.Net框架)都可以具有多种技能(例如WPF),并且每种技能都可以拥有多个用户,每个用户都有自己的能力。

我试图让它看起来像enter image description here

但是,我一直在输出的末尾添加用户名,从而破坏表格布局或者用户名一起丢失。正如您在此处所见enter image description here

这是我的观看代码

if( !empty($skills) )
    {
        $skill_group_name = '';
        $counter=0;
        //var_dump($skills);
        foreach ($skills as $row)
        {
             $section[$row['skill_group_name']][$row['skill_name']][$row['name']] = '<td>'.$row['competency_name'].'</td>'; //build entire section based on skill 
             $userID[$row['name']] = $row['user_id']; //get user id
        }
            $staff_name = '';
            $countStaff = '';

        foreach($section as $title => $all)
        {

            // Wrap it
            echo '<div class="panel panel-default">'."\n\r";
            echo '<div class="panel-heading"><a href="#" data-target=".skill'.++$counter.'" data-toggle="collapse">'."\n\r" . $title;
            echo '</a></div>'."\n\r";

            echo '<table class="table table-striped table-hover table-condensed skill'.$counter.' collapse">'."\n\r";

            foreach ($all as $skill => $skill_value)
            {
                foreach ($skill_value as $name => $competency)
                {
                    if($name != $countStaff)
                    {
                        echo "<colgroup></colgroup>";
                        $countStaff = $name;
                    }
                }
            }

            echo '<thead>'."\n\r".'<tr>'."\n\r";
            echo '<th></th>'."\n\r";

            foreach ($all as $skill => $skill_value)
            {

                foreach ($skill_value as $name => $competency)
                {

                     if($name != $staff_name)
                     {
                         echo '<th><h2><a href="/users/'.$userID[$name].'">'.$name.'</a></h2></th>'."\n\r";

                     }
                        $staff_name = $name;
                }
            }

            echo '</tr>'."\n\r";
            echo '</thead>'."\n\r"; 
            echo '<tbody>'."\n\r";

            foreach ($all as $skill => $skill_value)
            {
                echo '<tr>'."\n\r";
                echo '<td>'.$skill .'</td>'."\n\r";

                foreach ($skill_value as $key => $value)
                {
                    echo $value;
                }
                echo '</tr>'."\n\r";
            }

            echo '</tbody>'."\n\r";

            echo '</table>'."\n\r";
            echo '</div>'."\n\r";
        }

    } else {
        echo "no skills avaliable";            
    }

我在这里做错了什么?是否有一种更简单的方法来将数组中的结果集分组,而不是像我一样?

0 个答案:

没有答案