簡體   English   中英

將來自多維PHP數組的父節點的值串起來

[英]String together values of parent nodes from a multidimensional PHP array

我有一張工作場所及其父母的表格。 每個父母可以有任意數量的級別。

id    workplace              parent
1     WCHN                   0
2     Acute Services         1
3     Paediatric Medicine    2
4     Surgical Services      2
5     Nursing and Midwifery  1
6     Casual Pool            5
7     Clinical Practice      5

我需要創建一個單獨的選擇輸入,列出工作場所及其所有父工作場所,如下所示:

<select>
    <option>WCHN > Acute Services > Paediatric Medicine</option>
    <option>WCHN > Acute Services > Surgical Services</option>
    <option>WCHN > Nursing and Midwifery > Casual Pool</option>
    <option>WCHN > Nursing and Midwifery > Clinical Practice</option>
</select>

通過修改此解決方案,我已經能夠將我的平面列表轉換為多維數組並輸出值對,但沒有完整路徑。

<?php
public function list_all_workplaces()
{
    $temp = $result = array();
    $list = array();

    // Get the data from the DB
    $table = mysql_query("SELECT * FROM workplaces");

    // Put it into one dimensional array with the row id as the index
    while ($row = mysql_fetch_assoc($table)) {
      $temp[$row['workplace_id']] = $row;
    }

    // Loop the 1D array and create the multi-dimensional array
    for ($i = 1; isset($temp[$i]); $i++)
    {
        if ($temp[$i]['parent'] > 0)
        {
            $tmpstring = ($temp[$i]['workplace']); // workplace title

            // This row has a parent
            if (isset($temp[$temp[$i]['parent']])) {

                $list[$i] = $temp[$temp[$i]['parent']]['workplace']." > ".$tmpstring; 
                //example output: Acute Services > Paediatric Medicine

                // The parent row exists, add this row to the 'children' key of the parent
                $temp[$temp[$i]['parent']]['children'][] =& $temp[$i];

            } else {
                // The parent row doesn't exist - handle that case here
                // For the purposes of this example, we'll treat it as a root node
                $result[] =& $temp[$i];
            }
        } else {
            // This row is a root node
            $result[] =& $temp[$i];
        }

    }
    // unset the 1D array
    unset($temp);

    //Here is the result
    print_r($result);
}

示例print_r()輸出:

[1] => WCHN > Acute Services
[2] => Acute Services > Paediatric Medicine

我從哪里開始將所有父工作場所納入選擇選項?

如果沒有返回值,我不明白你如何使用你的函數。

如果您的陣列是正確構建的,並且您想要它的方式以及您要求將結果放入html選擇字段,那么這就是最簡單的部分。

讓我們快速回到你的功能。 您沒有在函數中列出返回$ result,因此在調用時它不會返回任何內容。

您需要將其添加到要啟動的函數末尾。

然后,您將遍歷數組以開始html處理。

$data = list_all_workplaces()
foreach( $data as $key => $value )
{
    $options = "<option value='{$key}'>{$value}</option>";
}
echo "<select>{$options}</select>";

我無法看到你的代碼如何產生你所說的print_r輸出,因為你沒有使用你創建的字符串。 但是沿着這些方向的東西應該讓你在路上 - 它會產生你需要的字符串。

用以下內容替換for循環:

foreach ($temp as $i => $details)
{
    $parentID = $details['parent'];
    $tmpstring = ($details['workplace']);
    if ($parentID > 0 && isset($temp[$parentID]))
    {
        $temp[$parentID]['children'][] =& $temp[$i];
        while ($parentID > 0 && isset($temp[$parentID]))
        {
            $tmpstring = $temp[$parentID]['workplace']." > ".$tmpstring;
            $parentID = $temp[$parentID]['parent'];
        }
    }
    $result[] = $tmpstring;
}

正如@Syx所說,你需要從你的函數中返回一些東西,可能是$result ,然后用它來生成你的<select>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM