PHP将简单数组转换为更复杂的关联数组

时间:2016-04-01 07:10:39

标签: php arrays

我从DB的选择会返回:

select dt.name,dt.asset4_code,dt.org_id,dt.year_max,dt.oa_perm_id,
        count(CASE WHEN date_part('year', dt.time_stamp) = 2002 THEN 1 end) AS Year2002,
        count(CASE WHEN date_part('year', dt.time_stamp) = 2003 THEN 1 end) AS Year2003,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2004 THEN 1 end) AS Year2004,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2005 THEN 1 end) AS Year2005,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2006 THEN 1 end) AS Year2006,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2007 THEN 1 end) AS Year2007,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2008 THEN 1 end) AS Year2008,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2009 THEN 1 end) AS Year2009,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2010 THEN 1 end) AS Year2010,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2011 THEN 1 end) AS Year2011, 
                count(CASE WHEN date_part('year', dt.time_stamp) = 2012 THEN 1 end) AS Year2012,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2013 THEN 1 end) AS Year2013,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2014 THEN 1 end) AS Year2014,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2015 THEN 1 end) AS Year2015 ,
                count(CASE WHEN date_part('year', dt.time_stamp) = 2016 THEN 1 end) AS Year2016

from
 (
    select companies.name,companies.asset4_code,companies.org_id,companies.year_max,companies.oa_perm_id,value_scopes.time_stamp,row_number(*)
    over (partition by sdp.dp_code order by sdp.dp_code desc ) as rn
     FROM 
                companies companies INNER JOIN value_scopes value_scopes ON value_scopes.company_id = companies.company_id
                JOIN dp_values dp ON value_scopes.value_scope_id=dp.value_scope_id
                JOIN dp_content_definition cd ON dp.dp_content_definition_id=cd.dp_content_definition_id 
                JOIN dp_definition def ON def.dp_definition_id=cd.dp_definition_id 
                right outer join strategic_data_point sdp on def.dp_code=sdp.dp_code 
                where 
        date_part('year', value_scopes.time_stamp)='2012'
                 and value_scopes.is_partial='f'
                and dp.no_answer='f'
                and (dp.dp_value_s IS  not  NULL or dp.dp_value_n IS  not  NULL)
                and companies.asset4_code='35'
                and def.dp_code like '%En%'

 ) dt 
where rn < 2  group by dt.name,dt.asset4_code,dt.org_id,dt.year_max,dt.oa_perm_id order by dt.name;

正如你所看到的,它基本上是第一个项目bin_full,分为第一个字母,第二个字母,接下来的2个数字,接下来的2个数字,最后是最后2个数字。模式是相同的,我需要将每个级别组合在一个关联数组中。

如果我这样做,我主要得到我想要的结果,但它需要调整,我不知道该怎么做..

Array
(
    [0] => Array
        (
            [bin_full] => AA010101
            [letter1_zone] => A
            [letter2_aisle] => A
            [letter34_bay] => 01
            [letter56_level] => 01
            [letter78_bin] => 01
        )

    [1] => Array
        (
            [bin_full] => AA010102
            [letter1_zone] => A
            [letter2_aisle] => A
            [letter34_bay] => 01
            [letter56_level] => 01
            [letter78_bin] => 02
        )

    [2] => Array
        (
            [bin_full] => AA010201
            [letter1_zone] => A
            [letter2_aisle] => A
            [letter34_bay] => 01
            [letter56_level] => 02
            [letter78_bin] => 01
        )

这导致:

foreach ($data_full as $row) {
    foreach ($row as $key2 => $value2) {
        $data_array[$row['letter1_zone']][$row['letter2_aisle']][$row['letter34_bay']][$row['letter56_level']][$row['letter78_bin']] = $value2;
    }
}

我需要最后一个数组:

Array
(
    [A] => Array
        (
            [A] => Array
                (
                    [01] => Array
                        (
                            [01] => Array
                                (
                                    [01] => 01
                                    [02] => 02
                                )

                            [02] => Array
                                (
                                    [01] => 01
                                    [02] => 02
                                )

                            [03] => Array
                                (
                                    [01] => 01
                                )
                        )
                )
        )
)

不是

[0] => 01
[1] => 02

完整的,我需要这个:

[01] => 01
[02] => 02

由于

2 个答案:

答案 0 :(得分:0)

你所要做的就是替换它:

foreach ($data_full as $row) {
    foreach ($row as $key2 => $value2) {
        $data_array[$row['letter1_zone']][$row['letter2_aisle']][$row['letter34_bay']][$row['letter56_level']][$row['letter78_bin']] = $value2;
    }
}

用这个:

foreach ($data_full as $row) {
    $data_array[$row['letter1_zone']][$row['letter2_aisle']][$row['letter34_bay']][$row['letter56_level']][] = $row['letter78_bin'];
}

所以你用&#34;添加新元素&#34;:[]替换最后一个索引。它首先会添加索引0,然后是1,然后是2,等等......

答案 1 :(得分:-1)

    $data_array[$row['letter1_zone']][$row['letter2_aisle']][$row['letter34_bay']][$row['letter56_level']][] = $value2;