[英]Output with PHP the value of a variable or a pre-defined CONSTANT from a MySQL result string
[英]Building hierarchies from MySQL PHP when no Parent or Child IDs are pre-defined
我正在使用 MySQL 5.7,所以了解通用表表達式不可用,但我試圖找到一種方法來基於基於列的輸入構建層次結構。
例如,我的表由以下內容組成......
地區 | 辦公室 | 人 |
---|---|---|
區域 1 | 辦公室 1 | 員工 1 |
區域 1 | 辦公室 1 | 員工 2 |
區域 1 | 辦公室 2 | 員工 1 |
區域 2 | 辦公室 1 | 員工 1 |
區域 2 | 辦公室 2 | 員工 1 |
區域 2 | 辦公室 2 | 員工 2 |
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=049349ecdbf3369026e009dcb08b3c14
最初我曾詢問(在另一個已關閉的問題中)關於如何使用動態生成的 ID 和 ParentID 來實現這一點的方向,但如果沒有大量的子查詢,這似乎是不可能的。 換一種方式思考,這可以在 PHP 中實現,而不是構建一個數組嗎?
假設來自數據庫的結果是按照表...
<?php
$dbResponse = array(
array(
"Region" => "Region 1",
"Office" => "Office 1",
"Employee" => "Employee 1"
),
array(
"Region" => "Region 1",
"Office" => "Office 1",
"Employee" => "Employee 2"
),
array(
"Region" => "Region 1",
"Office" => "Office 2",
"Employee" => "Employee 1"
),
array(
"Region" => "Region 2",
"Office" => "Office 1",
"Employee" => "Employee 1"
)
); // etc. etc. ...........
// Transformation here
如何將其轉化為生產所需的 output?
$newOutput = array(
array(
"Item" => "Region 1",
"Children" => array(
"Item" => "Office 1",
"Children" => array(
"Item" => array("Employee 1", "Employee 2")
)
),
array(
"Item" => "Office 2",
"Children" => array(
"Item" => array("Employee 1")
)
),
),
array(
"Item" => "Region 2",
"Children" => array(
"Item" => "Office 1",
"Children" => array(
"Item" => array("Employee 1")
)
)
)
);
將嵌套轉換為數組的最簡單方法是 -
$newOutput = [];
foreach ($dbResponse as $row) {
$newOutput[$row['Region']][$row['Office']][] = $row['Employee'];
}
這看起來像 -
Array
(
[Region 1] => Array
(
[Office 1] => Array
(
[0] => Employee 1
[1] => Employee 2
)
[Office 2] => Array
(
[0] => Employee 1
)
)
[Region 2] => Array
(
[Office 1] => Array
(
[0] => Employee 1
)
[Office 2] => Array
(
[0] => Employee 1
[1] => Employee 2
)
)
)
但是,如果您確實需要實現問題中建議的結構,那么您可以執行以下操作 -
$newOutput2 = [];
// initialise 2 counters
$r = $o = 0;
$previousRow = null;
foreach($dbResponse as $row) {
if (is_null($previousRow)) {
// no counter manipulation required
} elseif ($previousRow['Region'] == $row['Region'] && $previousRow['Office'] != $row['Office']) {
$o++;
} elseif ($previousRow['Region'] != $row['Region']) {
$r++;
$o = 0;
}
$newOutput2[$r]['Item'] = $row['Region'];
$newOutput2[$r]['Children'][$o]['Item'] = $row['Office'];
$newOutput2[$r]['Children'][$o]['Children']['Item'][] = $row['Employee'];
$previousRow = $row;
}
這看起來像 -
Array
(
[0] => Array
(
[Item] => Region 1
[Children] => Array
(
[0] => Array
(
[Item] => Office 1
[Children] => Array
(
[Item] => Array
(
[0] => Employee 1
[1] => Employee 2
)
)
)
[1] => Array
(
[Item] => Office 2
[Children] => Array
(
[Item] => Array
(
[0] => Employee 1
)
)
)
)
)
[1] => Array
(
[Item] => Region 2
[Children] => Array
(
[0] => Array
(
[Item] => Office 1
[Children] => Array
(
[Item] => Array
(
[0] => Employee 1
)
)
)
[1] => Array
(
[Item] => Office 2
[Children] => Array
(
[Item] => Array
(
[0] => Employee 1
[1] => Employee 2
)
)
)
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.