2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用循環(huán)大大簡(jiǎn)化它foreach,尤其是每次獲取鍵和值以幫助構(gòu)建數(shù)組。
這也使用第一級(jí)的鍵explode()將結(jié)果添加到using ,但也只是將值添加到數(shù)組的末尾 using ... $newArray$newArray[$mainKey][][]
foreach ( $array as $mainKey => $elements ) {
foreach ( $elements as $subKey => $value ){
$newData = explode("_", $subKey);
$newData[] = $value;
$newArray[$mainKey][] = $newData;
}
}
用你的測(cè)試數(shù)據(jù)給出......
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
)
)
)
我只是注意到我丟失了第二個(gè)company_info數(shù)據(jù),所以這意味著值將始終是數(shù)組,除非你真的只在需要時(shí)才需要它們是數(shù)組。

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
$new_array=[];
foreach($array as $category => $tmp ){
foreach($tmp as $key => $value){
$exp = explode('_', $key);
$exp[] = $value;
$new_array[ $category ][] = $exp;
}
}
- 2 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)