0

こんにちは皆さん、ここで配列の抽出について少し助けが必要です。以下に私の配列を示します。

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

しかし、私はサブカテゴリを作成したいと思います。どうやってやるの?

4

2 に答える 2

3

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。出力を必要なものに合わせてインラインに保つには、出力前に適切な条件が整っていることを常に確認してください。

出力に表示されているのは、実際には連想キー「カテゴリ」を含まない配列のキーです。見つかった場合、最初の文字を出力しています...上記の条件で試してみてください。それでも最初の文字のみを出力している場合は、他の場所にエラーがあります。

于 2013-10-16T07:39:04.020 に答える