简体   繁体   English

如何使用foreach循环提取多维数组?

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

Hello guys I need a little help here about extracting an array, Here's my array below: 大家好,我在这里需要一些有关提取数组的帮助,这是我的下面的数组:

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


                                )

How can I extract my array using this format: 如何使用以下格式提取数组:

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

I have this code: 我有以下代码:

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{

                }

            }

The $categories_tree is my main array. $ categories_tree是我的主要数组。 Now if I run my code it will display: 现在,如果我运行代码,它将显示:

- Filipino
- Continental
- Asian
- Others

But i want to create a subcategory. 但是我想创建一个子类别。 How can i do that? 我怎样才能做到这一点?

Use array_walk_recursive to cycle through your nested arrays 使用array_walk_recursive循环遍历嵌套数组

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

array_walk_recursive($categories_tree, "output_categories");

This function cycles through each element of an array and applies a callback, in this case output_categories . 此函数循环遍历数组的每个元素并应用回调,在本例中为output_categories To keep the output inline with what you need always make sure there are suitable conditions in place before output. 为了使输出与所需内容保持一致,请始终确保在输出之前有适当的条件。

What you are seeing in your output is actually the key of an array that doesn't contain an associative key "category". 您在输出中看到的实际上是一个不包含关联键“ category”的数组的键。 When it is found it is outputting the first letter... try with the above condition, if it is still outputting the first letter only there is error elsewhere. 找到它时,它正在输出第一个字母...请尝试上述条件,如果它仍在输出第一个字母,则仅在其他地方有错误。

Create a recursive function 创建一个递归函数

untested example 未经测试的例子

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