PHP - 使用特定结构从数组中回显值

时间:2016-06-17 11:59:56

标签: php arrays

我有以下代码:

$secondaryCatArray = array();
foreach($premiumCatArraySets as $key => $item){
  $secondaryCatArray[$item['secondary-category']][$key] = $item;
}
print_r($secondaryCatArray);

提供以下输出:

Array
(
    [Bride and Groom] => Array
        (
            [0] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Video
                )

            [1] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Make Up and Hair
                )

            [6] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Photography
                )

            [9] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Wedding Dresses
                )

            [13] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Organisers
                )

        )

我希望上面的foreach循环的输出显示如下:

<h1>Weddings</h1>
<h2>Bride and Groom</h2>
<h3>Video</h3>
<h3>Make Up and Hair</h3>
<h3>Photography</h3>

我知道这可能很简单,但我不确定如何绕过它。

2 个答案:

答案 0 :(得分:1)

对于“新娘和新郎”数组中的每个数组,循环遍历此数组并使用h *标记作为数组键填充数组,该数组与您所需的类别相对应。

之后,您需要遍历新阵列,并为每个h *键构建输出:

        $category = array(
            'h1' => array(),
            'h2' => array(),
            'h3' => array()
        );
        $output = '';
        foreach ($secondaryCatArray as $brideAndGroom){
            foreach ($brideAndGroom as $array){
                foreach ($array as $categoryType => $value){
                    if($categoryType == 'primary-category' && !in_array($value, $category['h1'])){
                        $category['h1'][] = $value;
                    }
                    if($categoryType == 'secondary-category' && !in_array($value, $category['h2'])){
                        $category['h2'][] = $value;
                    }
                    if($categoryType == 'tertiary-category' && !in_array($value, $category['h3'])){
                        $category['h3'][] = $value;
                    }
                }
            }
        }

        foreach ($category as $h => $array){
            foreach ($array as $value){
                $output .= '<'.$h.'>'.$value.'</'.$h.'>';
            }
        }

        print_r($output);

答案 1 :(得分:0)

如果您只有一个主要和一个辅助类别,则可以简单地对<h1><h2>进行硬编码。

    echo"<h1>Weddings<\h1>";
    echo"<h2>Bride and Groom<\h2>";
    foreach($arr['Bride and Groom'] as $item){
         foreach($item as $key => $value){
              if ($key=='tertiary-category'){
                    echo "<h3>".$value."</h3>";
              }           
        }
    }

如果您有多个主要和次要类别,则可以使用按列排序功能对数组进行排序,然后再将其放入foreach循环中,这样您就可以使用简单的if / else控制显示顺序。有关详细信息,请参阅内联代码注释。

  $primaryCat="";
  $secondaryCat="";

//sort $array by tertiary, secondary then primary category
    array_sort_by_column($arr['Bride and Groom'], 'tertiary-category');
    array_sort_by_column($arr['Bride and Groom'], 'secondary-category');
    array_sort_by_column($arr['Bride and Groom'], 'primary-category');

    foreach($arr['Bride and Groom'] as $key => $item){
         foreach($item as $key => $value){
//new primary category, assign name of category to $primaryCat
              if ($key=='primary-category' && $primaryCat!=$value){
                        $primaryCat=$value;
                        unset($secondaryCat);
              }
//new secondary category, assign name of category to $secondaryCat
//echo name of category 1 and 2 with h1 and h2 tag, since the array has already been sorted alphabetically by column, h1 h2 tag will only appear once before all the name of tertiary category under those two parent categories are displayed.
              else if ($key=='secondary-category' && $secondaryCat!=$value){ 
                        $secondaryCat =$value;
                        echo "<h1>".$primaryCat."</h1>";
                        echo "<h2>".$secondaryCat."</h2>";
              }
              else if ($key=='tertiary-category'){
                    echo "<h3>".$value."</h3>";
              }                
        }
    }
//sort by column function    
    function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
        $sort_col = array();
        foreach ($arr as $key=> $row) {
            $sort_col[$key] = $row[$col];
        }        
        array_multisort($sort_col, $dir, $arr);
    }

示例数据

Array
(
    [Bride and Groom] => Array
        (
            [0] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Video
                )

            [1] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Make up and Hair
                )

            [2] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Bride and Groom
                    [tertiary-category] => Something else
                )

            [3] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Add on
                    [tertiary-category] => Aerial Photo
                )

            [4] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Add on
                    [tertiary-category] => Limo
                )

            [7] => Array
                (
                    [primary-category] => Weddings
                    [secondary-category] => Add on
                    [tertiary-category] => Something else
                )

            [5] => Array
                (
                    [primary-category] => Bridal
                    [secondary-category] => Bachelorette Party
                    [tertiary-category] => Video
                )

            [6] => Array
                (
                    [primary-category] => Bridal
                    [secondary-category] => Bachelorette Party
                    [tertiary-category] => Make up and Hair
                )

        )

)

点击样本数据输出的代码段

&#13;
&#13;
<h1>Bridal</h1><h2>Bachelorette Party</h2><h3>Make up and Hair</h3><h3>Video</h3><h1>Weddings</h1><h2>Add on</h2><h3>Aerial Photo</h3><h3>Limo</h3><h3>Something else</h3><h1>Weddings</h1><h2>Bride and Groom</h2><h3>Make up and Hair</h3><h3>Something else</h3><h3>Video</h3>
&#13;
&#13;
&#13;