How to create nested list from PHP array for dropdown select field?

后端 未结 2 1320
温柔的废话
温柔的废话 2021-01-27 16:45

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 consecu

相关标签:
2条回答
  • 2021-01-27 17:05

    I just solved your problem here on SO in another question

    just replace ++$r with $r+1

    the "dash" now works great.

    0 讨论(0)
  • 2021-01-27 17:17

    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),
    );
    
    0 讨论(0)
提交回复
热议问题