合并具有匹配值的多维数组

时间:2019-08-07 09:02:57

标签: php arrays

我有一个<a href="~/YourControllerName/OnGetDelete?id=@item.id" id="deleteBtn" class="btn bg-danger mr-1" onclick="return confirm('Are you sure you want to delete?');"><i class="fas fa-trash-alt text-white"></i>Delete</a> 的数组,如果import matplotlib.pyplot as plt plt.show()

$list

这是我想要实现的结果数组结构(JSON)(请注意,最外面的文件夹print_r($list);是固定的):

Array
(
    [0] => images/announcement
    [1] => images/background
    [2] => images/background/thumbnail
    [3] => images/character
    [4] => images/character/frame
    [5] => images/character/party
    [6] => images/particle
    [7] => images/story/background
    [8] => images/story/character/1016/face
    [9] => images/unit/thumbnail
    [10] => images/user_title/background
    [11] => ss/chapter_background
    [12] => ss/others/ability_board_s
    [13] => ss/others/common/back-light-2
    [14] => ss/others/common/back_light_s

使用一些代码,我能够将每个files字符串拆分为多维数组:

{
   "name":"files",
   "type":"folder",
   "path":"files",
   "items":[
      {
         "name":"images",
         "type":"folder",
         "path":"files\/images",
         "items":[
                     "name":"announcement",
                     "type":"folder",
                     "path":"files\/images\/announcement",
                     "items":[]
         ]
      },
      {
         "name":"ss",
         "type":"folder",
         "path":"files\/ss",
         "items":[
                     "name":"others",
                     "type":"folder",
                     "path":"files\/ss\/others",
                     "items":[
                                 "name":"sub",
                                 "type":"folder",
                                 "path":"files\/ss\/sub",
                                 "items":[]
                             ]
         ]
      },
      {
         "name":"sound",
         "type":"folder",
         "path":"files\/sound",
         "items":[

         ]
      },
      {
         "name":"New folder (4)",
         "type":"folder",
         "path":"files\/New folder (4)",
         "items":[

         ]
      },
      {
         "name":"New folder (5)",
         "type":"folder",
         "path":"files\/New folder (5)",
         "items":[

         ]
      },
      {
         "name":"New folder (6)",
         "type":"folder",
         "path":"files\/New folder (6)",
         "items":[

         ]
      }
   ]
}

生成的$list看起来像这样:

function explodeToNestedArray($delimiter, $key, $dir)
    { 
        $value = array();
        $keys = explode($delimiter, $key, 2);

        $value["name"]=$keys[0];
        $value["type"]= "folder";
        $value["path"]= $dir . "/" . $keys[0];
        if ($keys[1] == null) $value["items"] = array();
        else
        $value["items"] = explodeToNestedArray("/", $keys[1], $dir . "/" . $keys[0]);
        return $value;
    }
  $list2 = array();
  foreach ($list as $value) {
    $list2[] = explodeToNestedArray("/", $value, "");
  }
      print_r($list2);

如果子数组[“ item”]元素具有相同的$list2,我该如何合并它们在Array ( [0] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => announcement [type] => folder [path] => /images/announcement [items] => Array ( ) ) ) [1] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => background [type] => folder [path] => /images/background [items] => Array ( ) ) ) [2] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => background [type] => folder [path] => /images/background [items] => Array ( [name] => thumbnail [type] => folder [path] => /images/background/thumbnail [items] => Array ( ) ) ) ) [3] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => character [type] => folder [path] => /images/character [items] => Array ( ) ) ) [4] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => character [type] => folder [path] => /images/character [items] => Array ( [name] => frame [type] => folder [path] => /images/character/frame [items] => Array ( ) ) ) ) [5] => Array ( [name] => images [type] => folder [path] => /images [items] => Array ( [name] => character [type] => folder [path] => /images/character [items] => Array ( [name] => party [type] => folder [path] => /images/character/party [items] => Array ( ) ) ) ) ..........etc* ) 中,这样我才能得到所需的结果? 使用$list2最终将所有子数组分为不同的类型,名称,路径和项数组。

2 个答案:

答案 0 :(得分:2)

我认为这可以满足您的需求。它遍历列表的每个元素,在输出数组的list元素中查找每个键。如果找不到,它将在输出中创建一个新条目。如果找到,则我们继续在树上向下确定在哪里添加键的下部。请注意,对于叶子元素,我们添加了type中的file,如果您希望将其设为folder,只需删除该行上的条件(即$element['type'] = 'folder';)。

$output = array('name' => 'files', 'type' => 'folder', 'path' => 'files', 'items' => array());

function explodeToNestedArray($delimiter, $key, &$output) {
    $keys = explode($delimiter, $key, 2);
    if (($k = array_search($keys[0], array_column($output['items'], 'name'))) === false) {
        // need to create an element
        $element = array('name' => $keys[0], 'path' => $output['path'] . "/{$keys[0]}", 'items' => array());
        $element['type'] = (count($keys) == 1) ? 'file' : 'folder';
        if (count($keys) == 2) explodeToNestedArray($delimiter, $keys[1], $element);
        $output['items'][] = $element;
    }
    else {
        // any children to add?
        if (count($keys) == 2) {
            explodeToNestedArray($delimiter, $keys[1], $output['items'][$k]);
            // change the type to folder in case it was previously a leaf
            $output['items'][$k]['type'] = 'folder';
        }
    }
}

foreach ($list as $node) {
    explodeToNestedArray('/', $node, $output);
}

输出内容太长,无法在此处显示,但是您可以在此demo on 3v4l.org中看到它。

答案 1 :(得分:0)

稍等一下,请在评论中让我知道它是否有效,我将继续回答。

function explodeToNestedArray($delimiter, $list, $dir) {

    $data = array();

    foreach($list as $key) {

        $value = array();
        $keys = explode($delimiter, $key, 2);

        $value["name"] = $keys[0];
        $value["type"] = "folder";
        $value["path"] = $dir . "/" . $keys[0];

        $items = array($keys[1]);
        $value["items"] = ($keys[1] == null ? array() : explodeToNestedArray("/", $items, $dir . "/" . $keys[0]));

        if(!isset($data[$keys[0]])) $data[$keys[0]] = array();
        $data[$keys[0]][] = $value;

    }

    return $data;

}

$list = explodeToNestedArray("/", $list, "");

print_r($list);