简体   繁体   English

如何在特定关键元素处将数组添加到多维数组

[英]How to add an array into a multidimensional array at a certain key element

Given a multidimensional array that represents a directory structure I would like to insert a new item at a certain key element. 给定一个表示目录结构的多维数组,我想在某个关键元素处插入一个新项。

This is the array: 这是数组:

$array = [
    [
        'key'=> 'b005f8da-208a-406a-8710-48dd5d3e598b',
        'title' => 'Title',
    ],
    [
        'key'=> 'ca0cd753-1f25-4e1a-b6fc-2e8c0e3aed0e',
        'title' => 'Title',
        'children' => [
            [
                'key'=> '605d755a-1ca0-4541-b24e-442bf19d432b',
                'title' => 'Title',
            ],
            [
                'key'=> 'b8d000a1-5ec4-469d-98aa-6f46647df611',
                'title' => 'Title',
                'children' => [
                    [
                        'key'=> '526b6329-6702-4e8b-83a8-564ba9044ff2',
                        'title' => 'Title',
                        'children' => [
                            [
                                'key'=> '0450f55f-88ec-476b-a317-f9c51f55baef',
                                'title' => 'Title',
                            ],
                            [
                                'key'=> 'a7f04948-9ef6-4dbc-9350-6db8ced30f01',
                                'title' => 'Title of target parent element',
                            ],
                            [
                                'key'=> 'c8e9e177-0be7-4d31-ad57-8d0677efae7f',
                                'title' => 'Title',
                            ], 
                        ],
                    ],
                ],
            ],
            [
                'key'=> '4aa9b0f4-1c75-4078-a1ec-4a92da20f14e',
                'title' => 'Title',
            ], 
        ],
    ],
];

This is the target key and the new element: 这是目标键和新元素:

$targetKey = 'a7f04948-9ef6-4dbc-9350-6db8ced30f01';
$newItem = [
    'key' => '8eb51f08-c8e2-47b0-9281-8c689649e8bf',
    'title' => 'Title of new element',
];

What is the best way to insert the new element as child of the target element (element within its children array)? 将新元素作为目标元素的子元素(其子元素数组中的元素)插入的最佳方法是什么?

$array can have an arbitrary number of nested levels. $array可以具有任意数量的嵌套级别。

Thanks y'all for your help! 谢谢大家的帮助!

I found the solution myself using the following recursive function. 我自己使用以下递归函数找到了解决方案。

function insertItem(array &$array, $key, array $newItem) {
    foreach($array as $k => &$v) {
        if(is_array($v)) {
            if(array_key_exists('key', $v) && $v['key'] == $key){
                $v['children'][] = $newItem;
                return true;
            }

            if(array_key_exists('children', $v)) {
                $res = insertItem($v['children'], $key, $newItem);
                if($res) return $res;
            }
        }
    }
}

insertItem($array, $targetKey, $newItem);

Try this: 尝试这个:

foreach($array AS $arr)
{
   if($arr['key'] == 'a7f04948-9ef6-4dbc-9350-6db8ced30f01')
   {
      $arr[] = array('key' => '8eb51f08-c8e2-47b0-9281-8c689649e8bf','title' => 'Title of new element',);
      break;
   }
}

Here's an example, to give you an idea: 这是一个例子,给你一个想法:

function recursiveKeyTargeting($array, $target_key, $element_to_add)
{
    foreach($array AS $arr)
    {
      if(is_array($arr['key']))
      {
          recursiveKeyTargeting($array['key'], $target_key, $element_to_add);
      }
      else if($arr['key'] == $target_key)
      {
         $arr[] = $element_to_add;
        break;
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM