在等于键上组合2个数组

时间:2014-04-07 18:34:45

标签: php arrays

我如何组合2个数组,其中State的一个= State的第二个AND ProgramName的1 = ProgramName的第二个给定以下数组?

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

我的尝试:

    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;
    }

正在制作:

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

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

            )

当我需要时:

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

            )

那么如何生成我需要的数组呢?我通常会假设包含数据的2个单独数组的array_merge,但是当stateprogramname对于每个元素相等时我需要合并

2 个答案:

答案 0 :(得分:2)

这是因为当你在数组中循环时,第二次迭代的结果正在破坏第一次。例如,您可能希望在将$responses[$i]->Age21Total分配给$response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['Age 21+']['Total Enrolled']之前检查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; } } 是否已定义或非空。

编辑:使其更明确:

{{1}}

答案 1 :(得分:1)

这样做吗?

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

        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['Total Enrolled'] = $responses[1]->Under21Total;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['FFS'] = $responses[1]->Under21FFS;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['MCO'] = $responses[1]->Under21MCO;
        $response_tree[$responses[$i]->ProgramName][$responses[$i]->State]['< Age 21']['Other'] = $responses[1]->Under21Other;
    }