简体   繁体   English

将来自多维PHP数组的父节点的值串起来

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

I have a table of workplaces and their parent ids. 我有一张工作场所及其父母的表格。 Each parent could have any number of levels. 每个父母可以有任意数量的级别。

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

I need to create a single select input that lists the workplaces with all their parent workplaces, a bit like this: 我需要创建一个单独的选择输入,列出工作场所及其所有父工作场所,如下所示:

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

By modifying this solution I've been able to turn my flat list into a multidimensional array and output pairs of values, but no the full paths. 通过修改此解决方案,我已经能够将我的平面列表转换为多维数组并输出值对,但没有完整路径。

<?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);
}

Example print_r() output: 示例print_r()输出:

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

Where do I go from here to get all of the parent workplaces into the select options? 我从哪里开始将所有父工作场所纳入选择选项?

I don't understand how you even use your function if there is no return value. 如果没有返回值,我不明白你如何使用你的函数。

If your array is built correctly and the way you want it and your asking about getting your results into an html select field then that's the easy part. 如果您的阵列是正确构建的,并且您想要它的方式以及您要求将结果放入html选择字段,那么这就是最简单的部分。

Let's get back to your function real quick . 让我们快速回到你的功能。 You don't have return $result listed in your function so it's not going to return anything when called. 您没有在函数中列出返回$ result,因此在调用时它不会返回任何内容。

You'll want to add that to the end of the function to start. 您需要将其添加到要启动的函数末尾。

You'll then loop through the array to start your html processing. 然后,您将遍历数组以开始html处理。

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

I can't see how your code will produce the print_r output you say you're seeing, as you're not ever using the string you create. 我无法看到你的代码如何产生你所说的print_r输出,因为你没有使用你创建的字符串。 But something along these lines should get you on your way - it generates the strings you need. 但是沿着这些方向的东西应该让你在路上 - 它会产生你需要的字符串。

Replace your for loop with the following: 用以下内容替换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;
}

As @Syx said, you'll need to return something from your function too, probably $result , and then use that to generate your <select> . 正如@Syx所说,你需要从你的函数中返回一些东西,可能是$result ,然后用它来生成你的<select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM