0

I'm trying to create a multidimensional array in php/mysql from my database and I want to group them based on months..

I have a database result like this

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

my array looks like this:

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 )
)

what i want is to have like this array group in months

$array_permonth = [[18,30,50,18],[20,30,26,72],[33,52,49,40]]

what can i do to make the array?

FZs
  • 16,581
  • 13
  • 41
  • 50
migF
  • 11
  • 2

2 Answers2

0

Assuming you are getting the $array in the format that you have specified. You can use the following code

$array_permonth  = array();

foreach( $array as $a ) {
    $month = $a['Bulan'];
    if( !isset ( $array_permonth[$month] ) ) {
        $array_permonth[$month] = array();
    }
    $array_permonth[$month][] = $a['numbers'];

}

If you don't want month as keys in $array_permonth, then you can use following code

$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'];

}
ascsoftw
  • 3,466
  • 2
  • 15
  • 23
  • `if( !isset ( $array_permonth[$month] ) ) { $array_permonth[$month] = array(); }` is not necessary in PHP. Child elements can be pushed with square brace syntax without declaring the parent element. – mickmackusa Aug 22 '22 at 00:58
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;
}
Sandhya Nair
  • 157
  • 2
  • 9