我想转换这个多维数组:
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"
.
.
)
)
在没有运气的情况下尝试了很多:(
非常感谢任何帮助!
答案 0 :(得分:1)
迭代外部数组,然后迭代内部数组。将每个值添加到结果中的索引数组中。
foreach ($your_array as $sub_array) {
foreach ($sub_array as $key => $value) {
$result[$key][] = $value;
}
}
答案 1 :(得分:1)
您实际上不必迭代整个数组,您只需迭代一个子数组并使用array_column来获取完整数组中的所有值。
Foreach($arr[0] as $key => $val){
$res[$key] = array_column($arr, $key);
}
请在此处查看:https://3v4l.org/SAZJd
如果数组大于你在这里显示的数字(你的点表示我认为) 这种方法比循环数组的每个单元要快得多。