0

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. 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?

2 Answers 2

3

Use array_walk_recursive to cycle through your nested arrays

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. 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". 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice! New function for me, array_walk_recursive +
What does it display Rochelle? This is a common use case for this function but it may need a little tweaking as data structures differ.
ok it display a series of characters and numbers.. like this 1 1 1 F 1 A f N 0 4 1 1 A 1 A a N 0 4 1 1 S 2 A s N 0
You need to put the right conditions in place, maybe a check to see if $item is an array first before checking a key exists would help. I haven't run this code so I can't debug it exactly.
0

Create a recursive function

untested example

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.