繁体   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