Person I have the following database structure:
id | parent_id | username
Where a parent can have n children and these children generate n children, this structure is already working, what I need is an example output:
[
(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, it already does much of what I need except the
mountMenuArrayList (array $ menuTotal, $ parent_id, array $ new = null)
I need it to output the array above, 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 should be done by the function montaMenuArrayList ():
[
(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;
}