<legend id='64BzX'><style id='64BzX'><dir id='64BzX'><q id='64BzX'></q></dir></style></legend>

    1. <i id='64BzX'><tr id='64BzX'><dt id='64BzX'><q id='64BzX'><span id='64BzX'><b id='64BzX'><form id='64BzX'><ins id='64BzX'></ins><ul id='64BzX'></ul><sub id='64BzX'></sub></form><legend id='64BzX'></legend><bdo id='64BzX'><pre id='64BzX'><center id='64BzX'></center></pre></bdo></b><th id='64BzX'></th></span></q></dt></tr></i><div id='64BzX'><tfoot id='64BzX'></tfoot><dl id='64BzX'><fieldset id='64BzX'></fieldset></dl></div>
      <tfoot id='64BzX'></tfoot>

      <small id='64BzX'></small><noframes id='64BzX'>

      • <bdo id='64BzX'></bdo><ul id='64BzX'></ul>

        如何从 PHP 数组为下拉选择字段创建嵌套列表?

        How to create nested list from PHP array for dropdown select field?(如何从 PHP 数组为下拉选择字段创建嵌套列表?)

          <tbody id='mY179'></tbody>

        <small id='mY179'></small><noframes id='mY179'>

      1. <i id='mY179'><tr id='mY179'><dt id='mY179'><q id='mY179'><span id='mY179'><b id='mY179'><form id='mY179'><ins id='mY179'></ins><ul id='mY179'></ul><sub id='mY179'></sub></form><legend id='mY179'></legend><bdo id='mY179'><pre id='mY179'><center id='mY179'></center></pre></bdo></b><th id='mY179'></th></span></q></dt></tr></i><div id='mY179'><tfoot id='mY179'></tfoot><dl id='mY179'><fieldset id='mY179'></fieldset></dl></div>
                <bdo id='mY179'></bdo><ul id='mY179'></ul>
                <tfoot id='mY179'></tfoot>
                  <legend id='mY179'><style id='mY179'><dir id='mY179'><q id='mY179'></q></dir></style></legend>

                • 本文介绍了如何从 PHP 数组为下拉选择字段创建嵌套列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的问题与本主题中描述的问题非常相似 从PHP数组为下拉选择字段创建嵌套列表 问题是,如果投资不连续,则错误地添加了破折号

                  My problem is very similar to the one described here in this topic Create nested list from PHP array for dropdown select field The problem is that if investments are not consecutive, then incorrectly adds a dash

                  function buildTree(Array $data, $parent = 0) {
                      $tree = array();
                      foreach ($data as $d) {
                          if ($d['parent'] == $parent) {
                              $children = buildTree($data, $d['id']);
                              // set a trivial key
                              if (!empty($children)) {
                                  $d['_children'] = $children;
                              }
                              $tree[] = $d;
                          }
                      }
                      return $tree;
                  }
                  
                  function printTree($tree, $r = 0, $p = null) {
                      foreach ($tree as $i => $t) {
                          $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' ';
                          printf("	<option value='%d'>%s%s</option>
                  ", $t['id'], $dash, $t['name']);
                          if ($t['parent'] == $p) {
                              // reset $r
                              $r = 0;
                          }
                          if (isset($t['_children'])) {
                              printTree($t['_children'], ++$r, $t['parent']);
                          }
                      }
                  }
                  

                  此解决方案适用于这样的结构数组

                  This solution works for such a structure array

                  $rows = array(
                      array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
                      array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
                      array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
                      array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
                      array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
                      array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
                      array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
                      array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 7),
                  );
                  

                  但不适用于此

                  $rows = array(
                      array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
                      array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
                      array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
                      array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
                      array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
                      array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
                      array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
                      array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 1),
                      array ('id' => 9, 'name' => 'another data', 'parent' => 1),
                  );
                  

                  我怎样才能改变它?对不起我的英语

                  How can I change it? sorry for my english

                  推荐答案

                  它不起作用,因为您没有正确设置父"属性.即使名称是Test 2.1",使用此算法,您也必须设置父索引.如果您将数组更改为此,它将起作用:

                  It does not work because you are not setting the "parent" attribute right. Even if the name is "Test 2.1", using this algorithm, you have to set the parent index. If you change your array to this, it'll work:

                  $rows = array(
                      array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
                      array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
                      array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
                      array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
                      array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
                      array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
                      array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
                      array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 7),
                      array ('id' => 9, 'name' => 'another data with no parent', 'parent' => 0),
                  );
                  

                  这篇关于如何从 PHP 数组为下拉选择字段创建嵌套列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <i id='YFC4y'><tr id='YFC4y'><dt id='YFC4y'><q id='YFC4y'><span id='YFC4y'><b id='YFC4y'><form id='YFC4y'><ins id='YFC4y'></ins><ul id='YFC4y'></ul><sub id='YFC4y'></sub></form><legend id='YFC4y'></legend><bdo id='YFC4y'><pre id='YFC4y'><center id='YFC4y'></center></pre></bdo></b><th id='YFC4y'></th></span></q></dt></tr></i><div id='YFC4y'><tfoot id='YFC4y'></tfoot><dl id='YFC4y'><fieldset id='YFC4y'></fieldset></dl></div>

                      <small id='YFC4y'></small><noframes id='YFC4y'>

                    1. <tfoot id='YFC4y'></tfoot>
                      • <bdo id='YFC4y'></bdo><ul id='YFC4y'></ul>
                        <legend id='YFC4y'><style id='YFC4y'><dir id='YFC4y'><q id='YFC4y'></q></dir></style></legend>
                          <tbody id='YFC4y'></tbody>