php对数组中的键与值进行合并处理

时间:2022-05-29 04:30:46
$res=array();
$re=array_count_values($month);
foreach( $re as $k=>$v){
    $arr['month_name'] = strval($k);
    $arr['month_counts']= strval($v);
    $res[]=$arr;
}

//处理前的数组结构
Array
(
    [10] =>12
    [11]=>46
    [12] =>2
    [01]=>1
)
//处理后的数组结构
Array(
    '0'=>array(
        'month_name'=>10,
        'month_counts'=>12
    ),
    '1'=>array(
        'month_name'=>11,
        'month_counts'=>46
    ),
    '2'=>array(
        'month_name'=>10,
        'month_counts'=>2
    ),
    '3'=>array(
        'month_name'=>01,
        'month_counts'=>1
    )
)