簡體   English   中英

如何使用foreach循環提取多維數組?

[英]How to extract a multidimensional array using foreach loop?

大家好,我在這里需要一些有關提取數組的幫助,這是我的下面的數組:

Array
(
    [11] => Array
        (
            [category_id] => 11
            [parent_id] => 1
            [id_path] => 1/11
            [category] => Filipino
            [position] => 10
            [status] => A
            [seo_name] => filipino-en
            [age_verification] => N
            [age_limit] => 0
            [age_warning_message] => 
            [level] => 1
            [subcategories] => Array
                (
                    [0] => Array
                        (
                            [category_id] => 42
                            [parent_id] => 11
                            [id_path] => 1/11/42
                            [category] => Adobo
                            [position] => 10
                            [status] => A
                            [seo_name] => adobo
                            [age_verification] => N
                            [age_limit] => 0
                            [age_warning_message] => 
                            [level] => 2
                        )

                    [1] => Array
                        (
                            [category_id] => 43
                            [parent_id] => 11
                            [id_path] => 1/11/43
                            [category] => Sinigang
                            [position] => 20
                            [status] => A
                            [seo_name] => singang
                            [age_verification] => N
                            [age_limit] => 0
                            [age_warning_message] => 
                            [level] => 2
                        )

                    [2] => Array
                        (
                            [category_id] => 44
                            [parent_id] => 11
                            [id_path] => 1/11/44
                            [category] => Kare-Kare
                            [position] => 30
                            [status] => A
                            [seo_name] => kare-kare
                            [age_verification] => N
                            [age_limit] => 0
                            [age_warning_message] => 
                            [level] => 2
                        )

                    [3] => Array
                        (
                            [category_id] => 45
                            [parent_id] => 11
                            [id_path] => 1/11/45
                            [category] => Dinuguan
                            [position] => 40
                            [status] => A
                            [seo_name] => dinuguan-en
                            [age_verification] => N
                            [age_limit] => 0
                            [age_warning_message] => 
                            [level] => 2
                        )

     [4] => Array
            (
                [category_id] => 4
                [parent_id] => 1
                [id_path] => 1/4
                [category] => Continental
                [position] => 20
                [status] => A
                [seo_name] => continental
                [age_verification] => N
                [age_limit] => 0
                [age_warning_message] => 
                [level] => 1
                [subcategories] => Array
                    (
                        [0] => Array
                            (
                                [category_id] => 12
                                [parent_id] => 4
                                [id_path] => 1/4/12
                                [category] => American
                                [position] => 10
                                [status] => A
                                [seo_name] => american
                                [age_verification] => N
                                [age_limit] => 0
                                [age_warning_message] => 
                                [level] => 2
                                [subcategories] => Array
                                    (
                                        [0] => Array
                                            (
                                                [category_id] => 54
                                                [parent_id] => 12
                                                [id_path] => 1/4/12/54
                                                [category] => Burger
                                                [position] => 10
                                                [status] => A
                                                [seo_name] => burger
                                                [age_verification] => N
                                                [age_limit] => 0
                                                [age_warning_message] => 
                                                [level] => 3
                                            )

                                        [1] => Array
                                            (
                                                [category_id] => 55
                                                [parent_id] => 12
                                                [id_path] => 1/4/12/55
                                                [category] => Steak
                                                [position] => 20
                                                [status] => A
                                                [seo_name] => steak-en
                                                [age_verification] => N
                                                [age_limit] => 0
                                                [age_warning_message] => 
                                                [level] => 3
                                            )

                                        [2] => Array
                                            (
                                                [category_id] => 56
                                                [parent_id] => 12
                                                [id_path] => 1/4/12/56
                                                [category] => Turkey
                                                [position] => 30
                                                [status] => A
                                                [seo_name] => turkey
                                                [age_verification] => N
                                                [age_limit] => 0
                                                [age_warning_message] => 
                                                [level] => 3
                                            )

                                        [3] => Array
                                            (
                                                [category_id] => 57
                                                [parent_id] => 12
                                                [id_path] => 1/4/12/57
                                                [category] => Salad
                                                [position] => 40
                                                [status] => A
                                                [seo_name] => salad
                                                [age_verification] => N
                                                [age_limit] => 0
                                                [age_warning_message] => 
                                                [level] => 3


                                )

如何使用以下格式提取數組:

-Filipino
  -  Adobo
  -  Sinigang
  -  Kare Kare
-Continental
  -  American
    - Burger
    - Steak
    - Turkey
    - Salad

我有以下代碼:

foreach($categories_tree as $k=>$v){

                if($v.subcategories){
                    echo "<div class=\"w900 h024 ";
                    echo $v['level'] == 1 ? "ml024" : "ml048";
                    echo '" >'."\n";
                    echo "<a href=\"\" class=\"w010 h010 p007 b03 d2 f1\">\n";
                    echo "<div class=\"it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03\"></div>\n";
                    echo "</a>\n";
                    echo "<div class=\"tw2 w839 h012 pt006 pb005 pl012 b04 ir3 ib3 jr01 jb01 kr02 kb02 f1\">\n";
                    echo "<input type=\"checkbox\" class=\"f1 w012 h012 m000 f1\" value=\"".$v['category_id']."\" />\n";
                    echo "<p class=\"ml012 f1\">".$v['category']."</p>\n";

                    echo "<div class=\"r3\"></div>\n";
                    echo "</div>\n";
                    echo "<div class=\"r3\"></div>\n";
                    echo "</div>\n";


                    //echo $v;

                }else{

                }

            }

$ categories_tree是我的主要數組。 現在,如果我運行代碼,它將顯示:

- Filipino
- Continental
- Asian
- Others

但是我想創建一個子類別。 我怎樣才能做到這一點?

使用array_walk_recursive循環遍歷嵌套數組

function output_categories($item, $index) {
    if (array_key_exists('category', $item) {
        echo $item['category'];
    }
}

array_walk_recursive($categories_tree, "output_categories");

此函數循環遍歷數組的每個元素並應用回調,在本例中為output_categories 為了使輸出與所需內容保持一致,請始終確保在輸出之前有適當的條件。

您在輸出中看到的實際上是一個不包含關聯鍵“ category”的數組的鍵。 找到它時,它正在輸出第一個字母...請嘗試上述條件,如果它仍在輸出第一個字母,則僅在其他地方有錯誤。

創建一個遞歸函數

未經測試的例子

function ReadCats($var)
  {
  foreach($var as $cat)
    {
    echo $cat['category'];
    if(isset($cat['subcategories'])
      {
      $ReadCats($cats['subcategories'];
      ]
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM