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



我有一个可变长度的键名数组我想用这些键名给数组赋值

例如,我有一个数组,其中列出了不同品牌的汽车和自行车的数量:
$vehicles = [
    'cars' => Array
    (
        'suzuki' => Array
        (
            'wagon' => 4,
            'baleno' => 2
        ),
        'honda' => Array
        (
            'civic' => 6
        )
    ),
    'bikes' => array
    (
        'raleigh' => 3,
        'scott' => 3
    )
];

我也有几个数组键名和值放在主数组:

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

输入所有这些值后,数组应该是这样的:

$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
            )
        )
    )
];

所以第一个数组在之前没有的地方添加了一个$key => $value对。第二个替换$array2末尾键的值,最后一个在没有键的情况下创建一个新数组。

如何以这种方式填充数组?

eval()将解决我所有的问题,但数组是从html创建的,所以它是一个巨大的安全风险。

您可以将$keys数组转换为与$vehicles相同的格式,然后递归地替换它们:

$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);
输出:

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

构造键/值对列表的方便方法(在我看来)如下:

// $vehicles is your initial array
$key_paths = [
  ['key_path' =>  ['cars', 'honda', 'jazz'], 'value' => 3],
  ['key_path' =>  ['bikes', 'scott'], 'value' => 1],
  ['key_path' =>  ['motorbikes', 'harley-davidson', 'dyna', 'street-bob'], 'value' => 2],
];

这里是使用array_slice函数和引用的解决方案:

foreach ($key_paths as $kPath) {
    $current = null;
    foreach (array_slice($kPath['key_path'], 0, -1) as $key) {
        if (is_null($current)) {
            if (!isset($vehicles[$key])) $vehicles[$key] = [];
            $current = &$vehicles[$key];
        } else {
            if (!isset($current[$key])) $current[$key] = [];
            $current = &$current[$key];
        }
    }
    $current[end($kPath['key_path'])] = $kPath['value'];
    unset($current);    // unsetting reference
}
print_r($vehicles);
输出:

Array
(
    [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
                        )
                )
        )
)

相关内容

  • 没有找到相关文章

最新更新