PHP recursive function, get children

0

Does anyone give me a hand? I need to create a recursive function, that the father obtains for me and if he has children, that is to say that he returns me slug: father and if he has children is slug: padre/hijo/hijos and so recursively.

MORE DETAILS

The idea is that we go through the array, show the father first, go back through the array, see if it has children and we concatenate it with the father and recursively, the output should be like this, let's agree that there is a good father, print

"father"

We run the function again checking if that father has childs, and if we have then we return back

"father / son"

We would rerun the function that verifies back if childs have childs if it should have returned like this

"father / son / son"

And so on until you generate a list of generated paths with the "slug" field, the final result would have to return the list

"father" "Father son" "father / son / son" "other father" "another father / son" "parentsinchild"

That's more or less the idea ... I do not know if I explain.

ANOTHER PROBLEM

A suggestion arose, well now what we are doing is to go through the array and go concatenating with your child, now we need to go concatenating only if you have children ... let's suppose that this is my array:

$rows2 = array(
array(
    'id' => 142,
    'name' => "Cate 1",
    'slug' => "Cate 1",
    'childs' =>

        array(
            'id' => 143,
            'name' => "Cate1 nivel 2",
            'slug' => "Cate1 nivel 2",
            'childs' => array()
        ),
        array( 
            'id' => 144,
            'name' => "Cate1 nivel 3",
            'slug' => "Cate1 nivel 3",
            'childs' => array()
        ),
        array(
            'id' => 145,
            'name' => "Cate1 nivel 4",
            'slug' => "Cate1 nivel 4",
            'childs' => array(
                'id' => 144,
                'name' => "Cate1 nivel 5",
                'slug' => "Cate1 nivel 5",
                'childs' => array()
            )
        )
)),

array(
'id' => 145,
'name' => "Cate 2",
'slug' => "Cate 2",
'childs' => array(
    'id' => 146,
    'name' => "Cate2 nivel 2",
    'slug' => "Cate2 nivel 2",
    'childs' => array()
))

);

If we look, the array of $ rows2 has a father with several children, now, what I try is to concatenate only 1 child with the father, it would be more or less like this:

Cate 1|Cate1 nivel 2
Cate 1|Cate1 nivel 3
Cate 1|Cate1 nivel 4

And in the case of having more than one level that verifies and concatenates me also with that level, example with the array id 145 that I would stay like this:

Cate 1|Cate1 nivel 4|Cate1 nivel nivel 5

That if it has more levels, you should be able to have a condition, I think.

This would be my array and my function that brings me the whole tree:

 $array = [
  {
    "id": 142,
    "name": "Nombre de la cate 1",
    "slug": "nombre-de-la-cate-1"
    "childs": [
      {
        "id": 143,
        "name": "Cate nivel 2",
        "slug": "cate-nivel-2",
        "childs": [
          {
            "id": 145,
            "name": "Cat nivel 3",
            "slug": "cat-nivel-3",

            "childs": [
              {
                "id": 146,
                "name": "Cat nivel 4",
                "slug": "cat-nivel-4",

                "childs": [
                  {
                    "id": 147,
                    "name": "Cay n5",
                    "slug": "cay-n5",

                    "childs": [
                      {
                        "id": 148,
                        "name": "Cat N6",
                        "slug": "cat-n6",

                        "childs": [],
                        "category_ad_count": 0,


                      },
                      {
                        "id": 149,
                        "name": "Cat N6",
                        "slug": "cat-n6",

                        "childs": [],
                        "category_ad_count": 0,


                      }
                    ],
                    "category_ad_count": 0,
                  }
                ],
                "category_ad_count": 0,
              }
            ],
            "category_ad_count": 0,
          }
        ],
        "category_ad_count": 0,
      },
      {
        "id": 144,
        "name": "Cat 2 nivel 2",
        "slug": "cat-2-nivel-2",
        "childs": [],
        "category_ad_count": 0,

      }
    ],    "category_ad_count": 0,
  },
  {
    "id": 3,
    "name": "Nombre de la cate 2",
    "slug": "nombre-de-la-cate-2"
    "childs": [
      {
        "id": 19,
        "name": "hijo",
        "slug": "hijo",
        "childs": [],
        "category_ad_count": 237,

      },
      {
        "id": 128,
        "name": "PRUEBA DE",
        "slug": "prueba-de",
        "childs": [],
        "category_ad_count": 0,

      },
      {
        "id": 17,
        "name": "hijo2",
        "slug": "hijo2",
        "childs": [],
        "category_ad_count": 198325,

      },
      {
        "id": 22,
        "name": "hijo3",
        "slug": "hijo3",
        "childs": [],
        "category_ad_count": 3579,

      },
      {
        "id": 124,
        "name": "prueba",
        "slug": "prueba",
        "childs": [],
        "category_ad_count": 0,

      },
      {
        "id": 24,
        "name": "hijo4",
        "slug": "hijo4",
        "childs": [],
        "category_ad_count": 6457,

      },
      {
        "id": 18,
        "name": "hijo5",
        "slug": "hijo5",
        "childs": [
          {
            "id": 135,
            "name": "hijo1",
            "slug": "hijo1",

            "childs": [],
                "category_ad_count": 0,


          },
          {
            "id": 136,
            "name": "hijo2",
            "slug": "hijo2",

            "childs": [],
                "category_ad_count": 0,


          },
          {
            "id": 137,
            "name": "hijo3",
            "slug": "hijo3",

            "childs": [],
                "category_ad_count": 0,


          },
          {
            "id": 138,
            "name": "hijo2",
            "slug": "hijo2",

            "childs": [
              {
                "id": 139,
                "name": "hijo1",
                "slug": "hijo1",

                "childs": [],
                        "category_ad_count": 0,


              },
              {
                "id": 140,
                "name": "hijo2",
                "slug": "hijo2",

                "childs": [],
                        "category_ad_count": 0,


              },
              {
                "id": 141,
                "name": "Otros",
                "slug": "otros",

                "childs": [],
                        "category_ad_count": 0,


              }
            ],
                "category_ad_count": 0,


          }
        ],
        "category_ad_count": 18733,

      },
      {
        "id": 23,
        "name": "sinhijos",
        "slug": "sinhijos",
        "childs": [],
        "category_ad_count": 1841,

      },
      {
        "id": 21,
        "name": "sinhijos2",
        "slug": "sinhijos2",
        "childs": [],
        "category_ad_count": 12172,

      }
    ],
    "category_ad_count": 241345,
  },
  {
    "id": 2,
    "name": "otros2",
    "slug": "otros2"
    "childs": [
      {
        "id": 25,
        "name": "hijo1",
        "slug": "hijo1",
        "childs": [],
        "category_ad_count": 58783,

      },
      {
        "id": 26,
        "name": "hijo2",
        "slug": "hijo2",
        "childs": [],
        "category_ad_count": 2557,

      },
      {
        "id": 27,
        "name": "hijo3",
        "slug": "hijo3",
        "childs": [],
        "category_ad_count": 22637,

      },
      {
        "id": 28,
        "name": "hijo4",
        "slug": "hijo4",
        "childs": [],
        "category_ad_count": 8761,

      }
    ],
    "category_ad_count": 176319,
  }
];

The expected result would be:

padre
padre/hijo
padre/hijo/hijo 

This is the function that I'm going through:

function category($array){
    $inicio = '';
    foreach ($array as $k => $v) {
        $inicio .= '<li>'.((is_array($v)) ? $k.category($v) : $v).'</li>';
    }
    return '<ul>'.$inicio.'</ul>';
}

echo category($array);
    
asked by Roberto 22.01.2018 в 15:34
source

1 answer

0

Does this help you? I'm not sure how you want to show the data

Assuming the array is:

$rows = array(
    array(
    'id' => 142,
    'name' => "Cate 1",
    'slug' => "Cate 1",
    'childs' => array(
        'id' => 143,
        'name' => "Cate1 nivel 2",
        'slug' => "Cate1 nivel 2",
        'childs' => array(
            'id' => 144,
            'name' => "Cate1 nivel 3",
            'slug' => "Cate1 nivel 3",
            'childs' => array()
        )
    )),
    array(
    'id' => 145,
    'name' => "Cate 2",
    'slug' => "Cate 2",
    'childs' => array(
        'id' => 146,
        'name' => "Cate2 nivel 2",
        'slug' => "Cate2 nivel 2",
        'childs' => array()
    ))
);

EDIT

Now I understand better what you want to achieve, I think that in the following way it would be ok:

$ant="";

foreach($rows as $row){
    array_walk_recursive($row, 'test_print', ["cadena"=>&$ant]);
    echo "\n";
    $ant="";
}

function test_print($item, $key, $ant){
    if($key == "name"){
        if(empty($ant["cadena"]))
            $ant["cadena"] .= $item;
        else
            $ant["cadena"] .= "|".$item;    
        echo  $ant["cadena"]."\n";      
    }
}

Here Update Sandbox with the new edition

    
answered by 22.01.2018 / 18:31
source