Ankita Patel
Ankita Patel

Reputation: 251

PHP : Dynamically get multidimentional array child element value

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 :

Upvotes: 2

Views: 139

Answers (2)

Mehak Sharma
Mehak Sharma

Reputation: 1

<?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);
                }               
            }
        }


?>

Upvotes: 0

Rahul
Rahul

Reputation: 18567

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
)

Upvotes: 1

Related Questions