我的数组很少,如下所示。实际上我有太多这样的阵列但是为了你的参考,我只打印了几个:
Array
(
[0] => lineItemData
[1] => name
)
Array
(
[0] => lineItemData
[1] => startDate
)
Array
(
[0] => lineItemData
[1] => endDate
)
Array
(
[0] => lineItemData
[1] => frequencyCapping
[2] => interval
)
Array
(
[0] => lineItemData
[1] => frequencyCapping
[2] => amount
)
Array
(
[0] => orderId
)
Array
(
[0] => isExternal
)
现在你可以在man中观察上面的数组,键值[lineItemData]
是常见的,并且它出现在oth索引处。现在我想创建一个新的数组,其中键为[lineItemData]
,而其他没有值[lineItemData]
的数组本身应该是新键,其他键应该是每个键下的键。我的问题可能让你困惑。所以我打印在所需的输出数组
Array
(
[lineItemData] => Array
(
[name] =>
[startDate] =>
[endDate] =>
[frequencyCapping] => Array
(
[interval] =>
[amount] =>
)
)
[orderId] =>
[isExternal] =>
)
答案 0 :(得分:2)
你可以这样做:
$data = [
['lineItemData', 'name'],
['lineItemData', 'startDate'],
['lineItemData', 'endDate'],
['lineItemData', 'frequencyCapping', 'interval'],
['lineItemData', 'frequencyCapping', 'amount'],
['orderId'],
['isExternal']
];
$result = [];
$pointer = &$result;
foreach($data as $keys)
{
foreach($keys as $key)
{
if(is_array($pointer) && !array_key_exists($key, $pointer))
{
$pointer[$key] = null;
}
$pointer = &$pointer[$key];
}
$pointer = &$result;
}
最终结果如下:
array(3) {
["lineItemData"]=>
array(4) {
["name"]=>
NULL
["startDate"]=>
NULL
["endDate"]=>
NULL
["frequencyCapping"]=>
array(2) {
["interval"]=>
NULL
["amount"]=>
NULL
}
}
["orderId"]=>
NULL
["isExternal"]=>
NULL
}
答案 1 :(得分:0)
也许是这样?
<?php
$super['lineItemData']['name'] = NULL;
$super['lineItemData']['startDate'] = NULL;
$super['lineItemData']['endDate'] = NULL;
$super['lineItemData']['frequencyCapping']['interval'] = NULL;
$super['lineItemData']['frequencyCapping']['amount'] = NULL;
$super['orderId'] = NULL;
$super['isExternal'] = NULL; ?>
我确信有人会变得狡猾,并找到一种方法在一个array
声明中实现这一点。我喜欢这个,因为我更容易管理。