php 怎么让多维数组中元素个数相同,并且键值也一样
发布网友
发布时间:2022-04-06 01:53
我来回答
共2个回答
热心网友
时间:2022-04-06 03:23
<?php
$arrs = array(
'4' => array(
'2010-10-10' => 42,
'2010-10-11' => 43,
'2010-10-12' => 44,
'2010-10-13' => 45
),
'3' => array(
'2010-10-09' => 33
),
'2' => array(
'2010-10-10' => 22,
'2010-10-11' => 23,
'2010-10-13' => 25
),
);
function changeIt($arrs){
$keys = array();
foreach($arrs as $arr){
$keys = array_merge($keys, array_keys($arr));
}
$keys = array_unique($keys);
sort($keys);
foreach($arrs as &$arr){
$default = current($arr);
foreach($keys as $key){
if(!isset($arr[$key])){
$arr[$key] = $default;
} else {
$default = $arr[$key];
}
}
ksort($arr);
}
return $arrs;
}
$arrs = changeIt($arrs);
print_r($arrs);
exit;
热心网友
时间:2022-04-06 04:41
只针对这道题还是其他可能也要考虑?