phparraysmultidimensional-arraygroupinghierarchical-data

Convert a 2d array into a hierarchical multidimensional array with conditionally determined subarray data


How can I convert a 2d array into a deeply nested multidimensional array where the lowest subarray is conditionally determined?

Array
(
[0] => stdClass Object
    (
        [Year] => 2013
        [State] => AK
        [ProgramName] => Medicaid
        [Age21Total] => 56324
        [Age21FFS] => 56324
        [Age21MCO] => 56324
        [Age21Other] => 
        [Under21Total] => 
        [Under21FFS] => 
        [Under21MCO] => 
        [Under21Other] =>
    )

[1] => stdClass Object
    (
        [Year] => 2013
        [State] => AK
        [ProgramName] => Medicaid
        [Age21Total] => 
        [Age21FFS] => 
        [Age21MCO] => 
        [Age21Other] => 
        [Under21Total] => 99085
        [Under21FFS] => 99085
        [Under21MCO] => 99085
        [Under21Other] =>
    )
 [2] => stdClass Object
    (
        [Year] => 2013
        [State] => AK
        [ProgramName] => CHIP
        [Age21Total] => 563241
        [Age21FFS] => 563241
        [Age21MCO] => 563241
        [Age21Other] => 
        [Under21Total] => 
        [Under21FFS] => 
        [Under21MCO] => 
        [Under21Other] =>
    )

[3] => stdClass Object
    (
        [Year] => 2013
        [State] => AK
        [ProgramName] => CHIP
        [Age21Total] => 
        [Age21FFS] => 
        [Age21MCO] => 
        [Age21Other] => 
        [Under21Total] => 990851
        [Under21FFS] => 990851
        [Under21MCO] => 990851
        [Under21Other] =>
    )
)

My attempts:

    for($i = 0; $i < $rCt; ++$i){
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['Total Enrolled'] = $responses[$i]->Age21Total;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['FFS'] = $responses[$i]->Age21FFS;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['MCO'] = $responses[$i]->Age21MCO;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['Other'] = $responses[$i]->Age21Other;
        
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['Total Enrolled'] = $responses[$i]->Under21Total;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['FFS'] = $responses[$i]->Under21FFS;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['MCO'] = $responses[$i]->Under21MCO;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['Other'] = $responses[$i]->Under21Other;
    }

are producing:

Array
(
[Medicaid] => Array
    (
        [AK] => Array
            (
                [Age 21+] => Array
                    (
                        [Total Enrolled] => 
                        [FFS] => 
                        [MCO] => 
                        [Other] => 
                    )

                [< Age 21] => Array
                    (
                        [Total Enrolled] => 99085
                        [FFS] => 99085
                        [MCO] => 99085
                        [Other] => 
                    )

            )

When I need:

Array
(
[Medicaid] => Array
    (
        [AK] => Array
            (
                [Age 21+] => Array
                    (
                        [Total Enrolled] => 56324
                        [FFS] => 56324
                        [MCO] => 56324
                        [Other] => 
                    )

                [< Age 21] => Array
                    (
                        [Total Enrolled] => 99085
                        [FFS] => 99085
                        [MCO] => 99085
                        [Other] => 
                    )

            )

So how can I produce the array I need? I normally would assume array_merge of 2 seperate arrays containing the data, but I would need the merge to happen when the state and programname are equal for each element


Solution

  • It's because when you're looping through your array, the results of the second iteration are trouncing the first. You'll probably want to check to see if $responses[$i]->Age21Total is defined or non-empty before assigning it to $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['Total Enrolled'], for example.

    Edit: To make it more explicit:

    for($i = 0; $i < $rCt; ++$i) {
        if (strlen($responses[$i]->Age21Total) != 0) {
            $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['Total Enrolled'] = $responses[$i]->Age21Total;
        }
    }