У меня есть массив, в котором есть данные о сотруднике с его идентификатором руководителя, и я должен построить организационную диаграмму, которая будет представлять иерархию отчетов, для которой мне нужен массив в многомерном массиве, я пробовал много способов, но не смог построить в соответствии с требованиями.Любой, пожалуйста, помогите мне с ответом.
Array
(
[0] => stdClass Object
(
[userID] => 336
[fullName] => S G
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274919.jpg
)
[1] => stdClass Object
(
[userID] => 316
[fullName] => S J
[employeeSupervisiorID] => 1
[employeePhoto] => 1537274885.jpg
)
[2] => stdClass Object
(
[userID] => 337
[fullName] => R K
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274940.jpg
)
[3] => stdClass Object
(
[userID] => 405
[fullName] => De B
[employeeSupervisiorID] => 1
[employeePhoto] => 1537274961.jpg
)
[4] => stdClass Object
(
[userID] => 418
[fullName] => J D
[employeeSupervisiorID] => 337
[employeePhoto] => 1537274978.jpg
)
[5] => stdClass Object
(
[userID] => 420
[fullName] => A S
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274997.jpg
)
[6] => stdClass Object
(
[userID] => 421
[fullName] => Sanjay j
[employeeSupervisiorID] => 1
[employeePhoto] => 1537275044.png
)
[7] => stdClass Object
(
[userID] => 423
[fullName] => Test User
[employeeSupervisiorID] => 336
[employeePhoto] => 1537275091.jpg
)
)
Теперь я должен преобразовать этот массив в режиме иерархии, как показано ниже
Array
(
[1] => Array
(
[0] => Array
(
[userID] => 316
[fullName] => S J
[employeeSupervisiorID] => 1
[employeePhoto] => 1537274885.jpg
[childern] => Array
(
[0] => Array
(
[userID] => 336
[fullName] => S G
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274919.jpg
[childern] => Array
(
[0] => Array
(
[userID] => 423
[fullName] => Test User
[employeeSupervisiorID] => 336
[employeePhoto] => 1537275091.jpg
)
)
)
[1] => Array
(
[userID] => 337
[fullName] => R K
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274940.jpg
[childern] => Array
(
[0] => Array
(
[userID] => 418
[fullName] => J D
[employeeSupervisiorID] => 337
[employeePhoto] => 1537274978.jpg
)
)
)
[2] => Array
(
[userID] => 420
[fullName] => A S
[employeeSupervisiorID] => 316
[employeePhoto] => 1537274997.jpg
)
)
)
[1] => Array
(
[userID] => 405
[fullName] => De B
[employeeSupervisiorID] => 1
[employeePhoto] => 1537274961.jpg
[childern] =>
)
[2] => Array
(
[userID] => 421
[fullName] => Sanjay j
[employeeSupervisiorID] => 1
[employeePhoto] => 1537275044.png
[childern] =>
)
)
)
Я написал два для каждого цикла, но это дает мне результатдо второго заказа.Заранее спасибо за ваш ответ.Вот мой код, который я пробовал.
$arrEmpSupervisiorIDs = [];
foreach ( $arrEmployeeDetails as $arrEmployeeDetail ) {
$arrEmpSupervisiorIDs[$arrEmployeeDetail->employeeSupervisiorID][] = $arrEmployeeDetail;
}
$arrOrganizationChart = [];
// Building Tree By admin user ID
foreach ( $arrEmpSupervisiorIDs[$intAdminUserID] as $key => $objEmpSupervisiorID ) {
$arrOrganizationChart[$intAdminUserID][$key] = (array) $objEmpSupervisiorID;
$arrOrganizationChart[$intAdminUserID][$key]['childern'] = $arrEmpSupervisiorIDs[$objEmpSupervisiorID['userID']];
}