分解数据并构建新数组
Explode data and build new array
我需要分解数组的键值并构建一个具有分解结果的新数组。下面的代码适用于一个子数组,我想我缺少一个 for 循环来处理数组值迭代。
该解决方案还应处理 'finance' 子数组数据,以便在新数组中展开并显示。
我在后期会有 9 个子数组,因此需要分解数据并将结果移动到新数组中。
我的代码
<?php
$array = [
'company_info' => [
'country_period_0' => 10,
'currency_period_0' => 20
],
'finance' => [
'values_period_0' => 30
]
];
$newArray = [];
for ($i=0; $i <= 1 ; $i++) {
$array_1 = $array['company_info'];
$arrayKeys = array_keys($array_1);
$arrayValues = array_values($array_1);
$keySplits = explode("_", $arrayKeys[$i]);
for ($i=0; $i <= 2 ; $i++) {
$newArray[] = $keySplits[$i];
}
$newArray[3] = $arrayValues[0];
}
print_r($newArray);
结果
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
想要的结果
['company_info]
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
Array(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
['finance']
Array(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
您可以使用 foreach
循环大大简化它,尤其是每次获取键和值以帮助构建数组。
这也使用 explode()
并使用 $newArray[$mainKey][]
将第一级的键将结果添加到 $newArray
,但也只是将值添加到数组的末尾使用 []
...
foreach ( $array as $mainKey => $elements ) {
foreach ( $elements as $subKey => $value ){
$newData = explode("_", $subKey);
$newData[] = $value;
$newArray[$mainKey][] = $newData;
}
}
用你的测试数据给出...
Array
(
[company_info] => Array
(
[0] => Array
(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
[1] => Array
(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
)
[finance] => Array
(
[0] => Array
(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
)
)
我刚刚注意到我丢失了第二个 company_info
数据,所以这意味着值将始终是数组,除非你真的只在需要时才需要它们。
$new_array=[];
foreach($array as $category => $tmp ){
foreach($tmp as $key => $value){
$exp = explode('_', $key);
$exp[] = $value;
$new_array[ $category ][] = $exp;
}
}
我需要分解数组的键值并构建一个具有分解结果的新数组。下面的代码适用于一个子数组,我想我缺少一个 for 循环来处理数组值迭代。
该解决方案还应处理 'finance' 子数组数据,以便在新数组中展开并显示。
我在后期会有 9 个子数组,因此需要分解数据并将结果移动到新数组中。
我的代码
<?php
$array = [
'company_info' => [
'country_period_0' => 10,
'currency_period_0' => 20
],
'finance' => [
'values_period_0' => 30
]
];
$newArray = [];
for ($i=0; $i <= 1 ; $i++) {
$array_1 = $array['company_info'];
$arrayKeys = array_keys($array_1);
$arrayValues = array_values($array_1);
$keySplits = explode("_", $arrayKeys[$i]);
for ($i=0; $i <= 2 ; $i++) {
$newArray[] = $keySplits[$i];
}
$newArray[3] = $arrayValues[0];
}
print_r($newArray);
结果
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
想要的结果
['company_info]
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
Array(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
['finance']
Array(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
您可以使用 foreach
循环大大简化它,尤其是每次获取键和值以帮助构建数组。
这也使用 explode()
并使用 $newArray[$mainKey][]
将第一级的键将结果添加到 $newArray
,但也只是将值添加到数组的末尾使用 []
...
foreach ( $array as $mainKey => $elements ) {
foreach ( $elements as $subKey => $value ){
$newData = explode("_", $subKey);
$newData[] = $value;
$newArray[$mainKey][] = $newData;
}
}
用你的测试数据给出...
Array
(
[company_info] => Array
(
[0] => Array
(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
[1] => Array
(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
)
[finance] => Array
(
[0] => Array
(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
)
)
我刚刚注意到我丢失了第二个 company_info
数据,所以这意味着值将始终是数组,除非你真的只在需要时才需要它们。
$new_array=[];
foreach($array as $category => $tmp ){
foreach($tmp as $key => $value){
$exp = explode('_', $key);
$exp[] = $value;
$new_array[ $category ][] = $exp;
}
}