Good, I'm with a problem explained more or less, I need to go through the array and go concatenating father and son, now we need to go concatenated only if you have children ... suppose 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 function I have so far ...
$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";
}
}