通过分组php回显多维数组

时间:2016-06-20 10:41:45

标签: php arrays multidimensional-array

$list = array(
        array(
        'header_name' => '1.',
        'header_database_name' => '2',
        'heading' => 'personal',
        ),

        array(
        'header_name' => '11. ',
        'header_database_name' => '22',
        'heading' => 'professional',        
        ),

        array(
        'header_name' => '33',
        'header_database_name' => '333',
        'heading' => 'personal',    
        ),
    );

foreach($ list as $ li)             {
            ?>
“>';} 别的($ li ['heading'] =='表演') {echo $ li ['header_name']。'
';} }

我需要通过标题对它进行分组来打印这个数组,例如标题为personal的那个应显示在个人标题下,即:

 Personal
    1
    33
    Professional
    11

我的PHP代码就像这样

foreach($ list as $ li)             {
            ?>
“>';} 别的($ li ['heading'] =='表演') {echo $ li ['header_name']。'
';} }

它是按组打印,但我还需要添加标题,但只有一次在顶部,例如现在如果我添加标题,它打印为个人1 专业2 peronal 3

但我需要 个人 1 2 专业的 2

4 个答案:

答案 0 :(得分:0)

如果要对数据进行分组,则需要对数组进行排序,请使用usort

function headingSort ($x, $y) {
    return strcasecmp($x['heading'], $y['heading']);
}

usort($myArray, 'headingSort');

答案 1 :(得分:0)

使用

$new_array = array();
foreach($list as $k=>$v)
{
    if(!in_array($v['header_name'], $new_array))
        $new_array[$v['heading']][] = $v['header_name'];
    else
        $new_array[$v['heading']] = $v['header_name'];
}
print_r($new_array);

答案 2 :(得分:0)

试试这个:

$list = array(
    array(
    'header_name' => '1.',
    'header_database_name' => '2',
    'heading' => 'personal',
    ),
    array(
    'header_name' => '11. ',
    'header_database_name' => '22',
    'heading' => 'professional',        
    ),
    array(
    'header_name' => '33',
    'header_database_name' => '333',
    'heading' => 'personal',    
    )
);

$personal = array();
$professional = array();

for($i=0;$i<count($list);$i++){
    if($list[$i]['heading']=="professional"){
        array_push($professional,$list[$i]);
    }else{
        array_push($personal,$list[$i]);
    }
}
$new = array(
        "personal"=>$personal,
        "professional"=>$professional
    );
echo "<pre>";
print_r($new);

结果将如下:

Array
(
[personal] => Array
    (
        [0] => Array
            (
                [header_name] => 1.
                [header_database_name] => 2
                [heading] => personal
            )

        [1] => Array
            (
                [header_name] => 33
                [header_database_name] => 333
                [heading] => personal
            )

    )

[professional] => Array
    (
        [0] => Array
            (
                [header_name] => 11. 
                [header_database_name] => 22
                [heading] => professional
            )

    )
)

答案 3 :(得分:0)

使用array_column(自PHP 5.5起可用),array_uniquearray_keysimplode函数的解决方案:

$headings = array_column($list, 'heading', 'header_name');

foreach (array_unique($headings) as $v) {
    echo ucfirst($v). PHP_EOL;
    echo implode("\n", array_keys($headings, $v)). PHP_EOL;    
}

输出:

Personal
1
33
Professional
11