0

I am looking for last three month state wise data.I have all state code array and last three month array with the following values,

$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");

Below is my result array fetch from the database,

    Array ( 
        [0] => Array ( 
            [month] => Nov 2016
            [count] => 150
            [state] => NJ
        ) 
        [1] => Array ( 
            [month] => Nov 2016
            [count] => 100
            [state] => va 
        ) 
    )
        I want result like below,

        Array(
                [Nj] => Array(
                        [0] => Array(
                                     [month] => Nov 2016
                                     [count] => 150
                                    )
                        [1] => Array(
                                     [month] => Dec 2016
                                     [count] => 0
                                     )
                         [2] => Array(
                                     [month] => jan 2017
                                     [count] => 0
                                    ) 
                   )
               [Ca] => Array(
                        [0] => Array(
                                     [month] => Nov 2016
                                     [count] => 0
                                    )
                        [1] => Array(
                                     [month] => Dec 2016
                                     [count] => 0
                                     )
                         [2] => Array(
                                     [month] => jan 2017
                                     [count] => 0
                                    ) 
                   )
                [va] => Array(
                        [0] => Array(
                                     [month] => Nov 2016
                                     [count] => 100
                                    )
                        [1] => Array(
                                     [month] => Dec 2016
                                     [count] => 0
                                     )
                         [2] => Array(
                                     [month] => jan 2017
                                     [count] => 0
                                    ) 
                   )
            )

and so on for all states.

I am trying to array array_search() and in_array() functions for each but it is not working.What I would like to accomplish to loop each state wise array.Below I am trying for looping,

foreach ( $result_array as $val ) {
    $month = array_search ( $val ['month'], $monthArray );
    $state = array_search ( $val ['state'], $stateArray );
    if ($val ['count'] == '' || $val ['count'] == 'NULL') {
        $countValue = 0;
    } else {
        $countValue = $val ['count'];
    }
    $final_array [] = $countValue;
}
2
  • All such patterns are not valid php: $val ['month']. Remove the blank character between variable name and brackets. Commented Feb 28, 2017 at 8:21
  • ok,I removed this blank space but expected result not getting. Commented Feb 28, 2017 at 8:27

2 Answers 2

1

Take a look at this simple example:

<?php
$input = [
    [
        'month' => "Nov 2016",
        'count' => "150",
        'state' => "NJ"
    ],
    [
        'month' => "Nov 2016",
        'count' => "100",
        'state' => "va"
    ],
    [
        'month' => "Dec 2016",
        'count' => "270",
        'state' => "NJ"
    ],
];
$output = [];
foreach (["Nj", "va", "Ca", "BS", "TS"] as $state) {
    $output[strtoupper($state)] = [];
};

array_walk($input, function($entry) use (&$output) {
    $output[strtoupper($entry['state'])][] = [
        'month' => $entry['month'],
        'count' => $entry['count']
    ];
});

print_r($output);

The result of the above code is:

Array
(
    [NJ] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 150
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 270
                )

        )

    [VA] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 100
                )

        )

    [CA] => Array
        (
        )

    [BS] => Array
        (
        )

    [TS] => Array
        (
        )

)

This appears to be the exact output structure you ask for.

Sign up to request clarification or add additional context in comments.

8 Comments

We have set of state and moth.We have to loop each state with each month set.
@bhagwanpawar Sure, I understood that from your question. That example does loop all entries in your given result set. Do you want to say that your result set does not contain entries for all combinations of state and month? So you have incomplete data?
We want to append zero result set those are not in resultant array
yes arkascha, In result set does not contain entries for all combinations.We are not getting data for all month and state.Those month and state data not getting we set zero result set.
@bhagwanpawar Just add those emty default entries in your resulting array as shown in the updated answer...
|
1
       $stateArray = array("Nj","va","Ca","BS","TS");
    $MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
    $fromDB =    Array (
        Array (
            'month' => 'Nov 2016',
            'count' => 150,
            'state' => 'NJ'
            ),
        Array (
            'month' => 'Nov 2016',
            'count' => 100,
            'state' => 'va'
            )
        );
    $info = array();
    foreach ($fromDB as $row){
        $info[strtoupper($row['state'])][$row['month']] = $row['count'];
    }
    $result = array();
    foreach ($stateArray as $state){
        foreach ($MonthArray as $month){
            $result[$state][]=array('month'=>$month, 'count'=>isset($info[strtoupper($state)][$month])?$info[strtoupper($state)][$month]:0);
        }
    }
print_r($result);

will output

Array
(
    [Nj] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 150
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 0
                )

            [2] => Array
                (
                    [month] => Jan 2017
                    [count] => 0
                )

        )

    [va] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 100
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 0
                )

            [2] => Array
                (
                    [month] => Jan 2017
                    [count] => 0
                )

        )

    [Ca] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 0
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 0
                )

            [2] => Array
                (
                    [month] => Jan 2017
                    [count] => 0
                )

        )

    [BS] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 0
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 0
                )

            [2] => Array
                (
                    [month] => Jan 2017
                    [count] => 0
                )

        )

    [TS] => Array
        (
            [0] => Array
                (
                    [month] => Nov 2016
                    [count] => 0
                )

            [1] => Array
                (
                    [month] => Dec 2016
                    [count] => 0
                )

            [2] => Array
                (
                    [month] => Jan 2017
                    [count] => 0
                )

        )

)

1 Comment

Thanks Андрей Танечник,This is what I want.Thanku u very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.