PHP - 按键/值拆分多维数组 [英] PHP - Split Multidimensional Array by key/value
本文介绍了PHP - 按键/值拆分多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想转换这个多维数组:
I want to convert this multidimensional array:
array(
[0] => array (
"interest_income-bank" => "520.541"
"total_interest_expense" => "145.791"
"net_interest_income" => "434.937"
"loan_loss_provision" => "135.664"
.
.
)
[1] => array (
"interest_income-bank" => "617.894"
"total_interest_expense" => "205.508"
"net_interest_income" => "506.510"
"loan_loss_provision" => "120.586"
.
.
)
)
到这样的数组:
array(
[interest_income-bank] => array (
"0" => "520.541"
"1" => "617.894"
.
.
)
[total_interest_expense] => array (
"1" => "145.791"
"2" => "205.508"
.
.
)
)
尝试了很多都没有运气:(
Tried a lot with no luck :(
任何帮助将不胜感激!
推荐答案
迭代外部数组,然后迭代内部数组.将每个值添加到结果中的索引数组.
Iterate the outer array, then the inner arrays. Add each value to an indexed array in your result.
foreach ($your_array as $sub_array) {
foreach ($sub_array as $key => $value) {
$result[$key][] = $value;
}
}
这篇关于PHP - 按键/值拆分多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文