如何获取Multi-Dim Array的格式输出

时间:2016-08-30 19:42:24

标签: php multidimensional-array

我有以下数组$cat_group_array

Array
(
    [0] => Array
        (
            [cat] => 01
            [pgroup] => 0010
            [pgroup_name] => STEAKS - STRIPLOINS
        )

    [1] => Array
        (
            [cat] => 01
            [pgroup] => 0015
            [pgroup_name] => STEAKS - RIBEYES
        )

    [2] => Array
        (
            [cat] => 01
            [pgroup] => 0020
            [pgroup_name] => STEAKS - T-BONE
        )

    [3] => Array
        (
            [cat] => 01
            [pgroup] => 0025
            [pgroup_name] => STEAKS - TENDERLOIN
        )

    [4] => Array
        (
            [cat] => 01
            [pgroup] => 0030
            [pgroup_name] => STEAKS - TOP SIRLOIN
        )

    [5] => Array
        (
            [cat] => 01
            [pgroup] => 0035
            [pgroup_name] => STEAKS - Other Varieties
        )
    [6] => Array
        (
            [cat] => 05
            [pgroup] => 0145
            [pgroup_name] => CHICKEN - Ground
        )

    [7] => Array
        (
            [cat] => 05
            [pgroup] => 0150
            [pgroup_name] => CHICKEN-BREADED
        )

    [8] => Array
        (
            [cat] => 05
            [pgroup] => 0155
            [pgroup_name] => CHICKEN BREASTS (Raw Products)
        )
)

我的目标是获得类似这样的输出,我在类别标题中显示组和组名称

Cat: 01

0010 - Steaks - Striploin
0015 - Steaks - Ribeyes
0020 - Steaks - T-Bone
0030 - Steaks - Top Sirloin
0035 - Steaks - Other Varieties

Cat: 05

0145 - Chicken Ground
0150 - Chicken Breaded
0155 - Chicken Breasts

我尝试了几件事,但还没有任何成果。我一直在尝试这样的事情,但我没有得到输出。

foreach ($cat_group_array as $key => $row) {     
    $test = $cat_group_array[$key]['cat'];
    echo "Category: " . $test;
    while ($cat_group_array[$key]['cat'] == $test) {
        echo "Pgroup #: " . $cat_group_array[$key]['pgroup'] .
        " Group Name = " . $cat_group_array[$key]['pgroup_name'] . "<br /><br />";    
    }
}

如果有人能指出我正确的方向,我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

由于您需要提前知道类别,您可以使用array_map从数组中提取它们。

$categories = array_map(function($cat) {
    return $cat['cat'];
}, $cat_group_array);

由于您只需了解唯一类别,因此可以使用array_unique清除任何重复的类别。

$categories = array_unique($categories);

现在,您可以使用$categories数组作为过滤器,使用array_filter获得所需的最终结果。

foreach($categories as $category) {
    $items = array_filter(function($item) use($category) {
        return $item['cat'] === $category;
    }, $cat_group_array);

    echo "Cat: $category\n\n";

    foreach($items as $item) {
        echo "$pgroup - $pgroup_name\n";
    }
    echo "\n\n";
}

最终结果应符合预期。

Cat: 01

0010 - Steaks - Striploin
0015 - Steaks - Ribeyes
0020 - Steaks - T-Bone
0030 - Steaks - Top Sirloin
0035 - Steaks - Other Varieties

Cat: 05

0145 - Chicken Ground
0150 - Chicken Breaded
0155 - Chicken Breasts

答案 1 :(得分:0)

你可以用几种方式做到这一点。我将提供一个解决方案,您不需要按类别对原始数组进行排序。它首先创建另一个按类别名称键入的数组,保存包含属于它的行的子数组。然后它迭代这些类别,每个类别迭代行:

foreach ($cat_group_array as $row) {
    $categories[$row["cat"]][] = $row;
}
foreach ($categories as $name => $groups ) {
    echo "\n<h3>Category: $name</h3>\n\n";
    foreach ($groups as $group ) {
        echo "<p>{$group['pgroup']} {$group['pgroup_name']}</p>\n";
    }
}

eval.in上看到它。