亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP - 切片嵌套數組

PHP - 切片嵌套數組

PHP
嚕嚕噠 2023-03-04 14:30:59
我有兩個不同長度的嵌套數組。我想根據第一個數組制作第二個數組的長度,請參閱下面的示例以了解情況。只需刪除第一個數組中不存在的所有項目。有時第二個數組比第一個數組有更多的值,在這種情況下我的樹結構中斷了。這些數組是嵌套數組,所以簡單的 array_slice 不起作用。這是數組的結構。第一個數組 "1": {    "id": "1",    "username": "username",    "children": [      {        "id": "-1",        "username": "NULL",        "children": [          {            "id": "-1",            "username": "NULL",            "children": [              {                "id": "-1",                "username": "NULL",                "children": []              }            ]          }        ]      }    ]  }第二陣列"157": {    "id": "157",    "username": "test1",    "children": [      {        "id": "158",        "username": "test1",        "children": [          {            "id": "159",            "username": "test2",            "children": [              {                "id": "160",                "username": "test3",                "children": []              },              {                "id": "160",                "username": "KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY",                "children": []              }            ]          }        ]      },      {        "id": "160",        "username": "KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY",        "children": [          {            "id": "159",,            "username": "KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY",            "children": [              {                "id": "161",                "username": "KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY",                "children": []              }            ]          }        ]      }    ]  }我想做的是完全刪除第一個數組中不存在的那些數組子項。我正在構建一個最多三層的二叉樹。第一個數組已經有一個空值的二叉樹。第二個數組是來自數據庫的數據,我只是使用 array_replace 將空數據替換為實際數據。在第二個數組比第一個數組有更多的值之前,這一直很好用。所以為了讓它工作,我必須刪除那些額外的元素。任何人都可以幫助我使那里的長度相同。任何幫助將不勝感激。提前致謝。
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

發生了 Stack Overflow 奇跡......我得到了一個遞歸代碼片段來處理第一遍!通常我要花一兩個小時才能寫出有用的東西。

我不知道我是否可以使它更緊/更好,或者它是否會在任何邊緣情況下失敗,但是:

  1. 它適用于您的樣本輸入

  2. 對我來說現在是午夜,我累了,我必須在早上工作

實際上,只要結構數組中存在相同級別的鍵,它就會同步遞歸地迭代每個數組并將條目數組的每個級別存儲到輸出數組。

代碼:(演示

function truncateRecursive($structure, $entry) {

    $output = [];

    while (($structureKey = key($structure)) !== null && ($entryKey = key($entry)) !== null) {

        $output[$entryKey] = !is_array($entry[$entryKey])

            ? $entry[$entryKey]

            : truncateRecursive($structure[$structureKey], $entry[$entryKey]);

        unset($structure[$structureKey], $entry[$entryKey]);    

    }

    return $output;

}


var_export(truncateRecursive($structure, $entry));

輸出:


array (

  157 => 

  array (

    'id' => '157',

    'username' => 'test1',

    'children' => 

    array (

      0 => 

      array (

        'id' => '158',

        'username' => 'test1',

        'children' => 

        array (

          0 => 

          array (

            'id' => '159',

            'username' => 'test2',

            'children' => 

            array (

              0 => 

              array (

                'id' => '160',

                'username' => 'test3',

                'children' => 

                array (

                ),

              ),

            ),

          ),

        ),

      ),

    ),

  ),

)


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號