合并多维数组中的重复键

时间:2014-12-04 02:13:46

标签: php arrays multidimensional-array

我有一个脚本循环从数据库表收集的一堆数据。我已经在StackOverflow上阅读了关于合并重复数组键的其他类似帖子,但它们似乎都不适用于我。使用下面的代码,我将所有已编译的数据构建到一个数组中:

$sql = 'SELECT * FROM '.$qstTable.' WHERE '.$type.'_qst_id = '.$answer['answer_qst'];
$result = $db->sql_query($sql);
$q = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$sql = 'SELECT * FROM '.$catTable.' WHERE '.$type.'_cat_clean = "'.$q[$type.'_qst_cat'].'"';
$result = $db->sql_query($sql);
$cat = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$daField = $cat[$type.'_cat_name'];

if(count($allQsts)){
    if(array_key_exists($daField, $allQsts)){
        $daData = array(
            'question' => array(
                'id' => $q[$type.'_qst_id'],
                'qst' => $q[$type.'_qst_qst'],
            ),
            'answer' => array(
                'id' => $answer['answer_id'],
                'type' => $answer['answer_input'],
                'content' => $answer['answer_content'],
                'q_type' => $type,
            )
        );
        array_push($allQsts[$daField], $daData);
    }else{
        $allQsts[$cat[$type.'_cat_name']][] = array(
            'question' => array(
                'id' => $q[$type.'_qst_id'],
                'qst' => $q[$type.'_qst_qst'],
            ),
            'answer' => array(
                'id' => $answer['answer_id'],
                'type' => $answer['answer_input'],
                'content' => $answer['answer_content'],
                'q_type' => $type,
            )
        );
    }
}else{
    $allQsts[$cat[$type.'_cat_name']][] = array(
        'question' => array(
            'id' => $q[$type.'_qst_id'],
            'qst' => $q[$type.'_qst_qst'],
        ),
        'answer' => array(
            'id' => $answer['answer_id'],
            'type' => $answer['answer_input'],
            'content' => $answer['answer_content'],
            'q_type' => $type,
        )
    );
}

这就是我的阵列在处理完毕后的结果:

Array ( 
    [Ancestry] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 1 
                [qst] => Has your family always lived in this country? Where did your family come from? How did they come here? 
            ) 
            [answer] => Array ( 
                [id] => 28 
                [type] => text 
                [content] => idk 
                [q_type] => life 
            ) 
        ) 
    ) 
) 
Array ( 
    [High School] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 158 
                [qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them? 
            ) 
            [answer] => Array ( 
                [id] => 30 
                [type] => video 
                [content] => v-0bd3d270-2f24-0132-cd89-12313914f10b 
                [q_type] => life 
            ) 
        ) 
    ) 
)
Array ( 
    [High School] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 124 
                [qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school? 
            ) 
            [answer] => Array ( 
                [id] => 36 
                [type] => text 
                [content] => Started HS in 1987 
                [q_type] => life 
            ) 
        ) 
    ) 
) 
Array ( 
    [Young Adult] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 213 
                [qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends? 
            ) 
            [answer] => Array ( 
                [id] => 39 
                [type] => video 
                [content] => v-7d59df50-3bda-0132-cda7-12313914f10b 
                [q_type] => life 
            ) 
        ) 
    ) 
) 
Array ( 
    [Young Adult] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 207 
                [qst] => After high school - did you go to college, join the miltary, or did you get a job? 
            ) 
            [answer] => Array ( 
                [id] => 40 
                [type] => text 
                [content] => went to college at ASU 
                [q_type] => life 
            ) 
        ) 
    ) 
) 
Array ( 
    [Multiple Sclerosis] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 1278 
                [qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic? 
            ) 
            [answer] => Array ( 
                [id] => 33 
                [type] => text 
                [content] => vg hjc 
                [q_type] => pack 
            ) 
        ) 
    ) 
)

但是,我想要做的是组合已经存在的数组中的节点,如下所示:

Array ( 
    [Ancestry] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 1 
                [qst] => Has your family always lived in this country? Where did your family come from? How did they come here? 
            ) 
            [answer] => Array ( 
                [id] => 28 
                [type] => text 
                [content] => idk 
                [q_type] => life 
            ) 
        ) 
    ) 
) 
Array ( 
    [High School] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 158 
                [qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them? 
            ) 
            [answer] => Array ( 
                [id] => 30 
                [type] => video 
                [content] => v-0bd3d270-2f24-0132-cd89-12313914f10b 
                [q_type] => life 
            ) 
        ) 
        [1] => Array ( 
            [question] => Array ( 
                [id] => 124 
                [qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school? 
            ) 
            [answer] => Array ( 
                [id] => 36 
                [type] => text 
                [content] => Started HS in 1987 
                [q_type] => life 
            ) 
        ) 
    ) 
)
Array ( 
    [Young Adult] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 213 
                [qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends? 
            ) 
            [answer] => Array ( 
                [id] => 39 
                [type] => video 
                [content] => v-7d59df50-3bda-0132-cda7-12313914f10b 
                [q_type] => life 
            ) 
        )
        [1] => Array ( 
            [question] => Array ( 
                [id] => 207 
                [qst] => After high school - did you go to college, join the miltary, or did you get a job? 
            ) 
            [answer] => Array ( 
                [id] => 40 
                [type] => text 
                [content] => went to college at ASU 
                [q_type] => life 
            ) 
        ) 
    ) 
)
Array ( 
    [Multiple Sclerosis] => Array ( 
        [0] => Array ( 
            [question] => Array ( 
                [id] => 1278 
                [qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic? 
            ) 
            [answer] => Array ( 
                [id] => 33 
                [type] => text 
                [content] => vg hjc 
                [q_type] => pack 
            ) 
        ) 
    ) 
)

如何修改上述代码才能执行此操作?

编辑 - 更新以包含我尝试压缩数组的一些代码: 在创建完整的$allQsts数组之后,我通过它循环并测试输出,但每个条目都被复制了更多。

$sortedIt = array();
foreach($allQsts as $m => $n){
    if(!isset($sortedIt[$m])){
        $sortedIt[$m] = array();
    }
    $sortedIt[$m] = $n;
}

我也一直在玩array_merge_recursive,但还没有到达任何地方。

1 个答案:

答案 0 :(得分:0)

假设这是在一个循环中运行并且$allQsts每次指向同一个数组,您可以大大简化代码:

$daField = $cat[$type.'_cat_name'];
$daData = array(
    'question' => array(
        'id' => $q[$type.'_qst_id'],
        'qst' => $q[$type.'_qst_qst'],
    ),
    'answer' => array(
        'id' => $answer['answer_id'],
        'type' => $answer['answer_input'],
        'content' => $answer['answer_content'],
        'q_type' => $type,
    )
);

if (array_key_exists($daField, $allQsts)) {
    $allQsts[$daField][] = $daData;
} else {
    $allQsts[$daField] = array($daData);
}

这本身应足以获得所需的数据结构。