Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将点符号数组扩展为完整的多维数组_Php_Arrays_Laravel_Laravel 5 - Fatal编程技术网

Php 如何将点符号数组扩展为完整的多维数组

Php 如何将点符号数组扩展为完整的多维数组,php,arrays,laravel,laravel-5,Php,Arrays,Laravel,Laravel 5,在我的Laravel项目中,我有一个点符号数组,我需要将其转换为多维数组 数组是这样的: $dotNotationArray = ['cart.item1.id' => 15421a4, 'cart.item1.price' => '145', 'cart.item2.id' => 14521a1, 'cart.item2.price' => '1245'];

在我的Laravel项目中,我有一个点符号数组,我需要将其转换为多维数组

数组是这样的:

$dotNotationArray = ['cart.item1.id' => 15421a4, 
                 'cart.item1.price' => '145',
                 'cart.item2.id' => 14521a1,
                 'cart.item2.price' => '1245'];
    'cart' => [
        'item1' => [
            'id' => '15421a4',
            'price' => 145
        ],
        'item2' => [
            'id' => '14521a1',
            'price' => 1245,
        ]
    ]
如何将其扩展为如下数组:

$dotNotationArray = ['cart.item1.id' => 15421a4, 
                 'cart.item1.price' => '145',
                 'cart.item2.id' => 14521a1,
                 'cart.item2.price' => '1245'];
    'cart' => [
        'item1' => [
            'id' => '15421a4',
            'price' => 145
        ],
        'item2' => [
            'id' => '14521a1',
            'price' => 1245,
        ]
    ]
我如何才能做到这一点?

在Laravel 6+中,您可以使用:

Arr::set方法使用“点”表示法在深度嵌套的数组中设置值:

使用照明\支持\Arr;
$multiDimensionalArray=[];
foreach($dotNotationArray作为$key=>$value){
Arr::set($multiDimensionalArray,$key,$value);
}
转储($multiDimensionalArray);
如果您使用的是Laravel5.x,则可以使用功能相同的

说明:

$dotNotationArray = ['cart.item1.id' => 15421a4, 
                 'cart.item1.price' => '145',
                 'cart.item2.id' => 14521a1,
                 'cart.item2.price' => '1245'];
    'cart' => [
        'item1' => [
            'id' => '15421a4',
            'price' => 145
        ],
        'item2' => [
            'id' => '14521a1',
            'price' => 1245,
        ]
    ]
Arr::set()
将点符号格式的键的值设置为指定的键,并输出一个数组,如
['products'=>['desk'=>['price'=>200]]
。因此,您可以在数组键上循环以获得多维数组。