1
Person I have the following database structure:
id | parent_id | username
Where a father can have n children and these children generate n children, this structure is already working, what I need is an example:
[
(int) 1 => 'User1 ',
(int) 2 => 'User1 > User2 ',
(int) 3 => 'User1 > User3 ',
(int) 4 => 'User3 > User4'
....
]
From it I can generate a Select in my application. The code below is working, already makes a good part of what I need, except the function
montaMenuArrayList(array $menuTotal , $parent_id, array $new = null )
I need it to generate the above array in its output, but it only generates the first two values
[
(int) 2 => 'User2 ',
(int) 3 => 'User2 > User3 '
]
My Input Array:
[
(int) 1 => [
(int) 2 => [
'id' => '2',
'parent_id' => '1',
'username' => 'User2'
],
(int) 10 => [
'id' => '10',
'parent_id' => '1',
'username' => 'User10'
]
],
(int) 2 => [
(int) 3 => [
'id' => '3',
'parent_id' => '2',
'username' => 'User3'
],
(int) 4 => [
'id' => '4',
'parent_id' => '2',
'username' => 'User4'
]
],
(int) 10 => [
(int) 11 => [
'id' => '11',
'parent_id' => '10',
'username' => 'User11'
],
(int) 12 => [
'id' => '12',
'parent_id' => '10',
'username' => 'User12'
]
],
....
]
What I need in the end is that the output is an array with this structure and that should be done by the montaMenuArrayList function():
[
(int) 1 => 'User1 ',
(int) 2 => 'User1 > User2 ',
(int) 3 => 'User1 > User3 ',
(int) 4 => 'User3 > User4'
....
]
My current Code:
public function criaArvoreRev($arvoreCompleta)
{
foreach($arvoreCompleta as $key => $rev){
$arvore[] = $this->organizaArvoreRev($arvoreCompleta, $rev['id']);
}
return $arvore;
}
public function organizaArvoreRev($revTotal, $parent_id = null)
{
$menuItem = [];
foreach ($revTotal as $user){
if(!empty($user) && ($user['parent_id'] == $parent_id || $user['id'] == $parent_id)){
$menuItem[$user['id']] = ['id' => $user['id'], 'parent_id' => $user['parent_id'], 'username' => $user['username']];
}
}
return $menuItem;
}
public function nivelArray(array $array)
{
$newArray = array();
foreach($array as $key => $val){
if( isset($val['id']) ){
$parent_id = $val["parent_id"];
$id = $val["id"];
$newArray[$parent_id][$id] = ['id' => $val["id"], 'parent_id' => $val["parent_id"], 'username' => $val["username"]];
}else{
foreach($val as $k => $v ){
$parent_id = $v["parent_id"];
$id = $v["id"];
$newArray[$parent_id][$id] = $v;
}
}
}
return $newArray;
}
public function encontraRev(array $arvore, $id, $newline = null)
{
foreach($arvore as $key => $rev){
if(array_key_exists($id, $rev)){
$newline = $rev[$id]['username'] . " > " . $newline;
if($key == 1) unset($arvore[$key][1]);
return $this->encontraRev($arvore, $rev[$id]['parent_id'], $newline);
}
}
return $newline;
}
public function montaMenuArrayList( array $menuTotal , $parent_id, array $new = null )
{
foreach($menuTotal[$parent_id] as $key => $val)
{
$id = $key;
$valor_display = $this->encontraRev($menuTotal, $id);
$valor_display = substr($valor_display, 0, -2);
if($key == 1) unset($menuTotal[$key][1]);
if( isset( $menuTotal[$key] ) ){
$new[$id] = $valor_display;
return $this->montaMenuArrayList($menuTotal, $id, $new);
}
}
return $new;
}
And what you’ve tried so far?
– gmsantos
Opa, I forgot to put the code, I mentioned it and forgot. Updated on the question.
– Laercio Nunes
Possible duplicate of PHP menu with N levels
– novic
I removed the other, because I managed to evolve with the code, and the other could not be clear in the question ended up generating doubts in who was trying to help me.
– Laercio Nunes