首頁>Program>source

我有那个陣列:

$a = array(
    "7" => array(
        "id" => 7,
        "parent" => 6
    ),
    "6" => array(
        "id" => 6,
        "parent" => 5
    ),
    "5" => array(
        "id" => 5,
        "parent" => 4
    ),
    "4" => array(
        "id" => 4,
        "parent" => 0
    ),
    "3" => array(
        "id" => 7,
        "parent" => 2
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

我想要的結果是:

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4,
                array(
                    "6" => array(
                        "id" => 6,
                        "parent" => 5,
                        array(
                            "7" => array(
                                "id" => 7,
                                "parent" => 6
                            )
                        )
                    )
                )
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

我使用的代碼是:

foreach($a as $v)
{
    if(isset($a[$v['PARENT']]))
    {
        $a[$v['PARENT']][$v['ID']] = $v;
        unset($a[$v['ID']]);
    }
}

我遇到的問题是我得到了這个結果:

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

而不是需要它的結果。

最新回復
  • 5月前
    1 #

    要解決您的問题,您需要正確地了解PHP中變數引用/別名的工作原理。

    請看下面的示例代碼,该代碼看起来与您的代碼没有太大不同,但是利用引用来訪問任何父物件,即使它已经"移動"了:

    # transform $flat into a tree:
    foreach($flat as $id => &$value)
    {
        # check if there is a parent
        if ($parentId = $value['parent'])
        {
            $flat[$parentId][0][$id] =& $value; # add child to parent
            unset($flat[$id]); # remove reference from topmost level
        }
    }
    unset($value); # remove iterator reference
    print_r($flat); # your tree
    

    $flat 現在包含 $flat的所有值 -但重新排序.演示。

  • 5月前
    2 #

    您確定輸出陣列正確吗? 当然關键 2 應该是 1的child (由於 2'parent'=>1 )? 如果不是這種情况,我將無法理解實際要執行的操作以及键之間如何相互關聯。

    如果 2 應该是 1的child ,這有效:

    $keep = array();
    foreach ($a as $k => &$v) {
      // Loop the array first time and create references to
      // structure the array how you want it
      if ($v['parent']) {
        $a[$v['parent']][0] = array($k => &$v);
      } else $keep[] = $k;
    }
    foreach ($a as $k => $v) {
      // Loop it again to get rid of non-root nodes from the root
      if (!in_array($k,$keep)) {
        unset($a[$k]);
      }
    }
    print_r($a);
    

  • PHP將HTML Charset編碼弄亂了
  • 如何在C#windows應用程式的表單之間傳遞值?