我正在尝试从数据库中的php / mysql中创建多维数组,我想根据月份对它们进行分组。
我有这样的数据库结果
Count | service availed | month
18 | blood test | january
30 | dental | january
50 | blood pressure | january
18 | medical | january
20 | blood test | february
30 | dental | february
26 | blood pressure | february
72 | medical | february
33 | blood test | march
52 | dental | march
49 | blood pressure | march
40 | medical | march
我的数组如下:
Array (
[0] => Array (
[numbers] => 18
[Service_availed] => Blood Pressure
[Bulan] => January )
[1] => Array (
[numbers] => 449
[Service_availed] => Blood Test
[Bulan] => January )
[2] => Array (
[numbers] => 442
[Service_availed] => Dental Service
[Bulan] => January )
[3] => Array (
[numbers] => 26
[Service_availed] => Medical Service
[Bulan] => January )
[4] => Array (
[numbers] => 1
[Service_availed] => Blood Pressure
[Bulan] => February )
[5] => Array (
[numbers] => 152
[Service_availed] => Blood Test
[Bulan] => February )
[6] => Array (
[numbers] => 9
[Service_availed] => Dental Service
[Bulan] => February )
[7] => Array (
[numbers] => 9
[Service_availed] => Medical Service
[Bulan] => February )
[8] => Array (
[numbers] => 350
[Service_availed] => Blood Test
[Bulan] => March )
[9] => Array (
[numbers] => 39
[Service_availed] => Dental Service
[Bulan] => March )
[10] => Array (
[numbers] => 4
[Service_availed] => Medical Service
[Bulan] => March )
)
我想要在几个月内拥有这个数组组
$array_permonth = [[18,30,50,18],[20,30,26,72],[33,52,49,40]]
我该怎么做才能制作阵列?
答案 0 :(得分:0)
假设您以指定的格式获取$ array。您可以使用以下代码
$array_permonth = array();
foreach( $array as $a ) {
$month = $a['Bulan'];
if( !isset ( $array_permonth[$month] ) ) {
$array_permonth[$month] = array();
}
$array_permonth[$month][] = $a['numbers'];
}
如果您不想在$ array_permonth中将month作为键,则可以使用以下代码
$array_permonth = array();
$monthKeys = array();
$i = 0;
foreach( $array as $a ) {
$month = $a['Bulan'];
if( !isset ( $monthKeys[$month] ) ) {
$monthKeys[$month] = $i;
$i++;
}
$array_permonth[$monthKeys[$month]][] = $a['numbers'];
}
答案 1 :(得分:0)
$array = array ( array ( 'numbers' => 18 , 'service_availed' => 'Blood Pressure' ,'bulan' => 'January' ),
array ( 'numbers' => 449 ,'service_availed' => 'Blood Test' ,'bulan' => 'January' ),
array ( 'numbers' => 442 ,'service_availed' => 'Dental Service' ,'bulan' => 'January' ),
array ( 'numbers' => 26 ,'service_availed' => 'Medical Service' ,'bulan' => 'January' ),
array ( 'numbers' => 1 ,'service_availed' => 'Blood Pressure' ,'bulan' => 'February' ),
array ( 'numbers' => 152 ,'service_availed' => 'Blood Test' ,'bulan' => 'February' ),
array ( 'numbers' => 9 ,'service_availed' => 'Dental Service' ,'bulan' => 'February' ),
array ( 'numbers' => 9 ,'service_availed' => 'Medical Service' ,'bulan' => 'February' ),
array ( 'numbers' => 350 ,'service_availed' => 'Blood Test' ,'bulan' => 'March' ),
array ( 'numbers' => 39 ,'service_availed' => 'Dental Service' ,'bulan' => 'March' ) ,
array ( 'numbers' => 4 ,'service_availed' => 'Medical Service' ,'bulan' => 'March' )
);
$array_permonth = groupArray("bulan", $array);
// Dump result
echo "<pre>" . var_export($array_permonth, true) . "</pre>";
$array_permonth = groupArrayByKeyValue("bulan","numbers", $array);
// Dump result
echo "<pre>" . var_export($array_permonth, true) . "</pre>";
function groupArray($key, $data) {
$result = [];
foreach($data as $val) {
if(array_key_exists($key, $val)){$result[$val[$key]][] = $val;}}
return $result;
}
function groupArrayByKeyValue($keyIndex, $valueIndex, $data) {
$result = [];
foreach($data as $val) {if(array_key_exists($keyIndex, $val)){$result[$val[$keyIndex]][] = $val[$valueIndex];}}
return $result;
}