1
How can I classify this array
for the value of the key level, even though the values are currently sequential, they will not always be?
array:67 [▼
0 => array:3 [▼
0 => array:8 [▼
"idPai" => 1
"id" => 93
"nome" => "Pesoa"
"nivel" => 2
"email" => "[email protected]"
"telefone" => "fone"
"endereco" => "endereco"
"imagem" => "uploads/avatars/1547216218.jpeg"
]
1 => array:8 [▶]
2 => array:8 [▶]
]
1 => array:3 [▼
0 => array:8 [▼
"idPai" => 93,
"id" => 96,
"nome" => "Pesoa",
"nivel" => 2,
"email" => "[email protected]",
"telefone" => "fone",
"endereco" => "endereco",
"imagem" => "uploads/avatars/1547216218.jpeg"
]
1 => array:8 [▶]
2 => array:8 [▶]
]
3 => array:3 [▶]
4 => []
5 => []
6 => []
I would first like to filter these records and ensure that the sequence of levels be followed, example, all the levels 1, then all the levels 2, then all the levels 3 and so it goes.
I tried to
usort($array, function ($a, $b){
if (count($a) > 0 and count($b) > 0) {
return strcmp($a["nivel"], $b["nivel"]);
}
});
But it didn’t work, as I can do to sort based on the value of the index level these guys?
The method that mounts the array is this
public function getMatrizRecursive($users = null, $nivel = 1)
{
$nivel++;
if ($nivel > 9) {
return $this->matriz;
}
$montaArrayRede = [];
foreach ($users as $p){
$children = User::where('id', '=', $p->posicao1)
->orWhere('id', '=', $p->posicao2)
->orWhere('id', '=', $p->posicao3)->get();
foreach($children as $pessoa){
$montaArrayRede[] = [
'idPai' => $p->id,
'id' => $pessoa->id,
'nome' => $pessoa->nome,
'nivel' => $nivel,
'email' => $pessoa->email,
'telefone' => $pessoa->telefone,
'endereco' => $pessoa->cidade,
'imagem' => $pessoa->logo_number
];
}
array_push($this->matriz, $montaArrayRede);
$this->getMatrizRecursive($children, $nivel);
}
return $this->matriz;
}
Note: some indexes return empty.
Wouldn’t it be more practical to do this in the search ? Puts the method you are using to generate this array.
– Bulfaitelo
@Bulfaitelo I edited the question and added the method, if you look it descends through all the descendants of a "Hildren" until arriving at the last and then back to the "current level"
– Diego Ananias