Sql 多链表

发布于 2024-10-20 09:03:47 字数 718 浏览 3 评论 0原文

我正在尝试显示可以采取某些步骤的顺序。有些步骤可以同时执行,而另一些步骤则必须按特定顺序执行。我已经将数据存储在 SQL 表中,只是希望能够将其提取到 PHP 数组或其他内容中,以便可以将其打印出来。

数据存储在1个sql表中,有2个字段。第一个是 stat(这是该块的编号),第二个是 prereq,它标识前一个块(这可能是其他一些统计数据)。如果 prereq 字段为空,则这是起点。结束点是没有其他行时。

第一个示例:

status_number    prereq
-------------    -------
3                NULL
4                3
5                4
6                4
7                5
7                6
8                7

概念上看起来像这样:

我正在考虑首先以可视方式打印此内容我想将数据放入 PHP 数组中,并使用嵌套数组来垂直排列 2 个统计数据(在本例中为 5 和 6)。因此,该数组将如下所示:(3,4,(5,6),7,8)。我怎样才能将数据转化为这种形式?感谢您的帮助!

I'm trying to display the order in which certain steps can be taken. Some steps can be taken simultaneously, while others have to follow in a particular order. I have the data already in an SQL table, and just want to be able to pull this into a PHP array or something so I can print it out.

The data is stored in 1 sql table, with 2 fields. The first is stat (which is the number of this block), the second is prereq, which identifies the predecessor (which would be some other stat). If the prereq field is null, this is the starting point. The ending point is when there is no other rows.

1st example:

status_number    prereq
-------------    -------
3                NULL
4                3
5                4
6                4
7                5
7                6
8                7

Looks like this conceptually:

I'm thinking to visually print this, first I want to get the data into a PHP array, with nested arrays for where I would have 2 stats vertically (in this case, 5 & 6). So, the array would look like: (3,4,(5,6),7,8). How can I get the data into this form? Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-10-27 09:03:47

我认为这应该有效,尽管我必须承认我还没有测试过它:

# get the data - let the DB handle the ordering
$sql = "SELECT status_number,prereq FROM t ORDER BY prereq, status_number"
$res = mysql_query($sql);

# initialize pre-loop stuff
$status_array = array();
$prev_prereq = '';

# loop through the results
while ($row = mysql_fetch_assoc($res))
{
  # check if the prereq is the same as the previous result, and if so...
  if ($prev_prereq == $row['prereq'])
  {
    # look at the last element in the array
    end($status_array);
    $lastIndex = key($status_array);

    # if it's not an array
    if (! is_array($status_array[lastIndex]))
    {
      #  make it one that contains the value that was in that spot
      $v = $status_array[lastIndex]
      $status_array[lastIndex] = array();
      status_array[$lastIndex][] = $v;
    }

    # then append the status to that array
    status_array[$lastIndex][] = $row['status_number'];

  } else
  # just append the latest status the the end of the status array
  {
    $status_array[] = $row['status_number'];
    $prev_prereq = $row['prereq'];
  }
}

I think this should work, though I have to admit I haven't tested it:

# get the data - let the DB handle the ordering
$sql = "SELECT status_number,prereq FROM t ORDER BY prereq, status_number"
$res = mysql_query($sql);

# initialize pre-loop stuff
$status_array = array();
$prev_prereq = '';

# loop through the results
while ($row = mysql_fetch_assoc($res))
{
  # check if the prereq is the same as the previous result, and if so...
  if ($prev_prereq == $row['prereq'])
  {
    # look at the last element in the array
    end($status_array);
    $lastIndex = key($status_array);

    # if it's not an array
    if (! is_array($status_array[lastIndex]))
    {
      #  make it one that contains the value that was in that spot
      $v = $status_array[lastIndex]
      $status_array[lastIndex] = array();
      status_array[$lastIndex][] = $v;
    }

    # then append the status to that array
    status_array[$lastIndex][] = $row['status_number'];

  } else
  # just append the latest status the the end of the status array
  {
    $status_array[] = $row['status_number'];
    $prev_prereq = $row['prereq'];
  }
}
笑忘罢 2024-10-27 09:03:47

在这里,它可能需要通过

/* fetch the status_numbers and prereqs */
$query = "SELECT status_number, prereq FROM status";
$rs = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_row($rs)) {
    $ids[] = $row[0];
    $fwd[$row[0]][] = $row[1];
}

/* find the endpoint(s) */
$ends = array();
foreach ($ids as $search) {
    $found = false;
    foreach ($fwd as $deps) {
        if (in_array($search, $deps)) {
            $found = true;
            break;
        }
    }
    if ($found)
        continue;
    $ends[] = $search;
}

/* sort the deps so we can string compare */
foreach ($fwd as &$deps)
    asort($deps);

    /* recursive resolve function */
function resolve($fwd, $id, &$output) {
    if (!is_null($id))
        array_unshift($output, $id);

    $count = count($fwd[$id]);
    if ($count == 0)
        return;

    if ($count > 1) {
        $subs = array();
        $groups = array();
        foreach ($fwd[$id] as $dep)
            $subs[$dep] = implode(',', $fwd[$dep]);

        foreach ($subs as $dep => $str1) {
            unset($subs[$index]);
            foreach ($subs as $index => $str2)
                if ($str1 == $str2) {
                    unset($subs[$index]);
                    $groups[$str1][] = $index;
                }
        }

        foreach ($groups as $ids => $group) {
            array_unshift($output, $group);
            $ids = explode(',', $ids);
            foreach ($ids as $id)
                resolve($fwd, $id, $output);
        }
    }
    else {
        resolve($fwd, $fwd[$id][0], $output);
    }
}

$output = array();
foreach ($ends as $end)
    resolve($fwd, $end, $output);
print_r($output);

输出进行一些调整:

Array
(
  [0] => 3
  [1] => 4
  [2] => Array
      (
          [0] => 5
          [1] => 6
      )

  [3] => 7
  [4] => 8
)

Here you go, it may need some tweaking though

/* fetch the status_numbers and prereqs */
$query = "SELECT status_number, prereq FROM status";
$rs = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_row($rs)) {
    $ids[] = $row[0];
    $fwd[$row[0]][] = $row[1];
}

/* find the endpoint(s) */
$ends = array();
foreach ($ids as $search) {
    $found = false;
    foreach ($fwd as $deps) {
        if (in_array($search, $deps)) {
            $found = true;
            break;
        }
    }
    if ($found)
        continue;
    $ends[] = $search;
}

/* sort the deps so we can string compare */
foreach ($fwd as &$deps)
    asort($deps);

    /* recursive resolve function */
function resolve($fwd, $id, &$output) {
    if (!is_null($id))
        array_unshift($output, $id);

    $count = count($fwd[$id]);
    if ($count == 0)
        return;

    if ($count > 1) {
        $subs = array();
        $groups = array();
        foreach ($fwd[$id] as $dep)
            $subs[$dep] = implode(',', $fwd[$dep]);

        foreach ($subs as $dep => $str1) {
            unset($subs[$index]);
            foreach ($subs as $index => $str2)
                if ($str1 == $str2) {
                    unset($subs[$index]);
                    $groups[$str1][] = $index;
                }
        }

        foreach ($groups as $ids => $group) {
            array_unshift($output, $group);
            $ids = explode(',', $ids);
            foreach ($ids as $id)
                resolve($fwd, $id, $output);
        }
    }
    else {
        resolve($fwd, $fwd[$id][0], $output);
    }
}

$output = array();
foreach ($ends as $end)
    resolve($fwd, $end, $output);
print_r($output);

Outputs:

Array
(
  [0] => 3
  [1] => 4
  [2] => Array
      (
          [0] => 5
          [1] => 6
      )

  [3] => 7
  [4] => 8
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文