douqiangchuai7674 2016-07-04 10:23
浏览 42
已采纳

使用键名称数组在多维数组中添加/编辑值

I have an array of key names of variable length and I want to use those names to assign a value to an array

for example, I have an array that lists the number of cars and bikes of different makes:

$vehicles = [
    'cars' => Array
    (
        'suzuki' => Array
        (
            'wagon' => 4,
            'baleno' => 2
        ),
        'honda' => Array
        (
            'civic' => 6
        )
    ),
    'bikes' => array
    (
        'raleigh' => 3,
        'scott' => 3
    )
];

I also have several arrays of key names along with values to put in the main array:

$keys1 = ['cars', 'honda', 'jazz'];
$value1 = 3;
$keys2 = ['bikes', 'scott'];
$value2 = 1;
$keys3 = ['motorbikes', 'harley-davidson', 'dyna', 'street-bob'];
$value3 = 2;

After inputting all these values the array should look like this:

$vehicles = [
    'cars' => Array
    (
        'suzuki' => Array
        (
            'wagon' => 4,
            'baleno' => 2
        ),
        'honda' => Array
        (
            'civic' => 6,
            'jazz' => 3
        )
    ),
    'bikes' => array
    (
        'raleigh' => 3,
        'scott' => 1
    ),
    'motorbikes' => Array
    (
        'harley-davidson' => Array
        (
            'dyna' => Array
            (
                'street-bob' => 2
            )
        )
    )
];

So the first array adds a $key => $value pair where there wasn't one before. The second one replaces the value of the key at the end of $array2 and the last one creates a new array when there isn't one to begin with.

How can I populate the array in this way?

eval() would solve all my problems but the arrays are created from html and so it is a huge security risk.

  • 写回答

2条回答 默认 最新

  • dtjo87679 2016-07-04 13:43
    关注

    You could transform your $keys array in the same format as your $vehicles and then replace them recursively :

    $vehicles = [
        'cars' => [
            'suzuki' => [
                'wagon'  => 1,
                'boleno' => 2
            ]
        ],
    
        'bikes' => [
            'scott' => 3
        ]
    
    ];
    
    $keys = ['cars', 'honda', 'jazz'];
    $value  = 3;
    
    function addValues($vehicles, $keys, $value)
    {
        $formatted = formatArray($keys, $value);
    
        return array_replace_recursive($vehicles, $formatted);
    }
    
    function formatArray($array, $value)
    {
        $format = function ($carry, $item) {
            return [$item => $carry];
        };
    
        return array_reduce(array_reverse($array), $format, $value);
    }
    
    $vehicles = addValues($vehicles, $keys, $value);
    var_dump($vehicles);
    

    Output:

    array (size=2)
      'cars' => 
        array (size=2)
          'suzuki' => 
            array (size=2)
              'wagon' => int 1
              'boleno' => int 2
          'honda' => 
            array (size=1)
              'jazz' => int 3
      'bikes' => 
        array (size=1)
          'scott' => int 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?