2

I want to get automatically children element value from array. It's multidimentional array format. You can see array format as like below

Note : Child element automatically generate. So, not want to static code logic.

I want to create category tree and that array structure looks like this below array format :

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_cat_name] => 0
            [cat_name] => Test 1
            [status] => 1
            [position] => 1
            [created_at] => 2019-09-03 09:27:45
            [updated_at] => 2019-09-03 11:00:54
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_cat_name] => 1
                            [cat_name] => Test 2
                            [status] => 1
                            [position] => 2
                            [created_at] => 2019-09-03 09:28:19
                            [updated_at] => 2019-09-03 11:01:00
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [parent_cat_name] => 2
                                            [cat_name] => Test 4
                                            [status] => 1
                                            [position] => 4
                                            [created_at] => 2019-09-03 09:35:20
                                            [updated_at] => 2019-09-03 11:01:03
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 5
                                                            [parent_cat_name] => 4
                                                            [cat_name] => Test 5
                                                            [status] => 1
                                                            [position] => 3
                                                            [created_at] => 2019-09-07 05:55:09
                                                            [updated_at] => 2019-09-07 05:55:09
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 3
            [parent_cat_name] => 0
            [cat_name] => Test 3
            [status] => 1
            [position] => 4
            [created_at] => 2019-09-03 09:35:10
            [updated_at] => 2019-09-03 11:00:58
        )

)

Used code :

public function buildTree(array $elements, $parentId = 0) {
        $branch = [];

        foreach ($elements as $element) {
            if ($element['parent_cat_name'] == $parentId) {
                $children = $this->buildTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

Actual Result :

  • Test 1
    • Test 2
      • Test 4
        • Test 5
  • Test 3
4
  • I want is not a question. Describe real problem that you have. Commented Sep 10, 2019 at 10:00
  • @u_mulder I can't able to generate actual result from this above array. Commented Sep 10, 2019 at 10:02
  • So, where's the code that you use? Commented Sep 10, 2019 at 10:03
  • Check my updated question. Commented Sep 10, 2019 at 10:04

2 Answers 2

1

Here is custom function to achieve the same,

function custom_function($data)
{
    $result = [];
    // checking if during recursion if data is array or not found
    if (is_array($data) && count($data) > 0) {
        $result[] = '<ul>';
        foreach ($data as $entry) {
            // checking if leaf node visited or not
            if (isset($entry['children'])) {
                $result[] = sprintf('<li>%s %s</li>', $entry['cat_name'], custom_function($entry['children']));
            } else {
                // leaf node visited just add name to li
                $result[] = sprintf('<li>%s</li>', $entry['cat_name']);
            }
        }
        $result[] = '</ul>';
    }
    // implode or ul to form a string
    return implode($result);
}
echo custom_function($arr);

I took the ref of my answer in the past which matches criteria to some extent.

Demo.

Output:-

<ul>
  <li>Test 1
    <ul>
      <li>Test 2
        <ul>
          <li>Test 4
            <ul>
              <li>Test 5</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Test 3</li>
</ul>

EDIT 1

function custom_function($data)
{
    $result = [];
    if (is_array($data) && count($data) > 0) {
        //$result[] = '<ul>';
        foreach ($data as $entry) {
            if (isset($entry['children'])) {
                $result[] = [$entry['cat_name'], custom_function($entry['children'])];
            } else {
                $result[] = $entry['cat_name'];
            }
        }
        //$result[] = '</ul>';
    }
    return ($result);
}
$data= custom_function($arr);
print_r($data);

Output

Array
(
    [0] => Array
        (
            [0] => Test 1
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Test 2
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Test 4
                                            [1] => Array
                                                (
                                                    [0] => Test 5
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Test 3
)
Sign up to request clarification or add additional context in comments.

6 Comments

It's return : Undefined index: children
I made changes, now refresh the page and check edited version of my answer.
It's look like perfect. Can you please return as array instead of li tags.
Your output matches with mine. That's all you want it right? If you want it so, remove last implode condition and use that array as per your requirement
You are great !! Thanks for help me. Accepted !!
|
0
<?php

        subarray($a);
        function subarray($aa){
            for($i=0;$i<count($aa);$i++){
                echo $aa[$i]['cat_name']."<BR>";
                if(array_key_exists("children",$aa[$i])){
                    $charr=$aa[$i]['children'];
                    subarray($charr);
                }               
            }
        }


?>

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.