我有一个包含月度信息的数组,如下所示:
[appr] => Array
(
[0] => Array
(
[month] => August
[approd] => 23
)
[1] => Array
(
[month] => September
[approd] => 546
)
[2] => Array
(
[month] => October
[approd] => 234
)
)
我希望输出如下
[appr] => Array(
August => 23,
September => 546,
October => 234
)
任何人都可以帮助我使用php实现这一目标。
答案 0 :(得分:2)
如果您正在寻找简单的单行解决方案,请使用array_column()和array_combine()函数。
试试这个:
$array['appr'] = array_combine(array_column($array['appr'], "month"), array_column($array['appr'], "approd"));
答案 1 :(得分:2)
只需在array
中循环并创建一个新数组
$array = array(array('month'=>'August','approd'=>'23'),array('month'=>'September','approd'=>'56'),array('month'=>'October','approd'=>'234'),);
$new = array();
foreach($array as $val) {
$new['appr'][$val['month']] = $val['approd'];
}
答案 2 :(得分:1)
使用array_column
函数的“单行”解决方案:
$arr['appr'] = array_column($arr['appr'], 'approd', 'month');
print_r($arr);
输出:
Array
(
[appr] => Array
(
[August] => 23
[September] => 546
[October] => 234
)
)
答案 3 :(得分:0)
另一种选择是使用<div class="container">
<div class="box a">
</div>
<div class="box b">
</div>
<div class="box c">
</div>
</div>
:
array_walk_recursive
最后输出结果:
<?php
$array = array(
0 => array(
'something',
),
1 => array(
'else',
)
);
// If the keys are unique
$newArray = array();
array_walk_recursive($array, function($v, $k) use (&$newArray) {
$newArray[$k] = $v;
});
// If you have duplicate keys
$newArray = array();
array_walk_recursive($array, function($v) use (&$newArray) {
$newArray[] = $v;
});