我一直未能(我一整天)將此結構格式化為樹以這種格式呈現,請參見附圖。
因為這沒有父和子,所以像pattern_type_name 這樣的鍵值字串的名稱就像父母一樣,而pattern_name 是孩子,而在pattern_name 下是表示結構的marker_description。
顯示的結構
任何幫助,將不勝感激
下面是我正在使用的代碼:
$data='[
{
"pattern_type_name":"Blood Sugar",
"pattern_name":"TEST",
"marker_description":"A\/G ratio"
},
{
"pattern_type_name":"Blood Sugar",
"pattern_name":"TEST",
"marker_description":"Albumin"
},
{
"pattern_type_name":"Blood Sugar",
"pattern_name":"TEST",
"marker_description":"Alk Phos"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"A\/G ratio"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"Albumin"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"Alk Phos"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"BUN"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"BUN\/Creat ratio"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"Calcium"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST3",
"marker_description":"Chloride"
},
{
"pattern_type_name":"Cardivascular",
"pattern_name":"TEST1",
"marker_description":"EX3DWSQ"
},
{
"pattern_type_name":"Red Blood Cell",
"pattern_name":"TEST4",
"marker_description":"FEXTAFIX"
}
]';
$related_patterns=json_decode($data,true);
$arrlengng= count($related_patterns);
$patternCount=0;
$filteredItems=array();
$current_pattern_type_name=$related_patterns[0]
['pattern_type_name'];
$current_pattern_name=$related_patterns[0]['pattern_name'];
function filterArrayByKeyValue($array, $key, $keyValue){
return array_filter($array, function($value) use ($key,
$keyValue) {
return $value[$key] == $keyValue;
});
}
for($contloop=0;$contloop < $arrlengng;$contloop ){
if ($related_patterns[$contloop]
['pattern_type_name']==$current_pattern_type_name){
echo '==Pattern Type: '.$related_patterns[$contloop]['pattern_type_name'].' valor='.$contloop.'<br>';
echo '===Name: '.$related_patterns[$contloop]['pattern_name'].'<br>';
$current_pattern_name=$related_patterns[$contloop]['pattern_name'];
$current_pattern_type_name=$related_patterns[$contloop]['pattern_type_name'];
$filteredItems = filterArrayByKeyValue($related_patterns, 'pattern_name', $current_pattern_name);
while($patternCount < count($filteredItems)){
echo '====marker_description: '.$filteredItems[$patternCount]['marker_description'].'<br>';
$patternCount ;
}
}
$patternCount=0;
echo $contloop.'------->'.$current_pattern_type_name.'------>'.$related_patterns[$contloop]['pattern_type_name'].'<br>';
}
uj5u.com熱心網友回復:
當您需要將資料從一種形式轉換為另一種形式時,請將其分解為以下步驟:
- 發現資料結構
- 你如何翻譯——元素如何從一個轉移到另一個
- 遍歷資料
- 構建新結構
所以我注意到資料的第一件事是它已經被打包成JSON,這非常方便。它的結構如何?
$data = '[
{
"pattern_type_name":"Blood Sugar",
"pattern_name":"TEST",
"marker_description":"A\/G ratio"
},
// ... snip
]';
$table = json_decode($data); // illustrating with objects, it's easier to understand
print_r($table);
產量:
Array
(
[0] => stdClass Object
(
[pattern_type_name] => Blood Sugar
[pattern_name] => TEST
[marker_description] => A/G ratio
)
[1] => stdClass Object
(
[pattern_type_name] => Blood Sugar
[pattern_name] => TEST
[marker_description] => Albumin
)
// etc
所以我立即看到每一行(索引陣列)都是一個物件,每一行都具有相同的屬性。你也可以這樣描繪。將每個屬性作為一列:
type name description
0 Blood Sugar TEST A\G ratio
1 Blood Sugar TEST Albumin
2 Blood Sugar TEST Alk Phos
3 Red Blood Cell TEST3 A/G ratio
4 Red Blood Cell TEST3 Albumin
5 Red Blood Cell TEST3 Alk Phos
6 Red Blood Cell TEST3 BUN
7 Red Blood Cell TEST3 BUN/Creat ratio
// etc
If you think of each column's value as a key, you could think of it this way (not using the row index):
$table['Blood Sugar']['TEST']['A\G ratio '];
$table['Blood Sugar']['TEST']['Albumin'];
$table['Blood Sugar']['TEST']['Alk Phos'];
$table['Red Blood Cell']['TEST3']['A/G ratio'];
$table['Red Blood Cell']['TEST3']['Albumin'];
$table['Red Blood Cell']['TEST3']['Alk Phos'];
$table['Red Blood Cell']['TEST3']['BUN'];
$table['Red Blood Cell']['TEST3']['BUN/Creat ratio'];
This isn't valid code, of course; it's not assigning values. What's important is the structure. (in fact the value doesn't matter at all; it could just be a boolean true.)
Hopefully now the translation has become obvious; as you traverse $table, the desired order is going to just fall in place:
$out = [];
foreach($tempTable as $type=>$names) {
$out[] = "-$type";
foreach($names as $name=>$descriptions) {
$out[] = "--$name";
foreach($descriptions as $description=>$bool) {
$out[] = "----$description";
}
}
}
print join("\n",$out);
Now that we have our plan, we need to prepare the data. Fortunately, the data is already in rows; all we need to do is make a new data structure with the values.
$table = json_decode($data, true); // using associative arrays this time
$tempTable = [];
foreach($tempTable as $row) {
$type = $row['pattern_type_name'];
$name = $row['pattern_name'];
$desc = $row['marker_description'];
$tempTable[$type][$name][$desc] = true; // value doesn't really matter
}
Note, this is where Barmar's suggestion comes into play; instead of building an intermediate structure, he suggests printing it as you go by comparing each iteration's value against the previous. I'm accomplishing the same thing by making the new array dedupe the keys.
Putting it all together, we get this
$tempTable = [];
$table = json_decode($data, true); // using associative arrays this time
foreach($table as $row) {
$type = $row['pattern_type_name'];
$name = $row['pattern_name'];
$desc = $row['marker_description'];
$tempTable[$type][$name][$desc] = true; // value doesn't really matter
// print "tempTable[][$type][$name][$desc] = true;\n";
}
// print_r($tempTable); // this is how I discovered that the index shouldn't be there!
// die;
$out = [];
foreach($tempTable as $type=>$names) {
$out[] = "-$type";
foreach($names as $name=>$descriptions) {
$out[] = "--$name";
foreach($descriptions as $description=>$bool) {
$out[] = "----$description";
}
}
}
print join("\n",$out);
Last note: I personally would not be satisfied with the nested foreach loops... the picket fence is a signal that it could be done better; but for this exercise I think it's sufficient.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439555.html
上一篇:嘗試/捕獲“在null時呼叫成員函式query()”錯誤
下一篇:進入php腳本的引數的安全性
