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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况