0

以下代码从 mySQL JOIN 中获取一个包含一系列行的数组,并将它们重组为一个多维数组。

 foreach($content as $cont){

    if(!$pagecontent){
        $pagecontent = $cont;
        $pagecontent['theme'] = array();
    }
    $themearray = array(
        'themeID' => $cont['themeID'],
        'theme_type' => $cont['theme_type'],
        'theme_file' => $cont['theme_file'],
        'theme_default' => $cont['theme_default'],
        'theme_title' => $cont['theme_title'],
        $cont['meta_field'] => $cont['meta_value']
    );

    array_push($pagecontent['theme'], $themearray);

}

打印的输出数组如下。

Array
(
    [contentID] => 9
    [type] => operations
    [title] => CTK Partners
    [parent] => 8
    [theme] => Array
    (
        [0] => Array
            (
                [themeID] => 2
                [theme_type] => general
                [theme_file] => logo
                [theme_default] => 1
                [theme_title] => CTT Group
                [src] => uploads/img/clogo.png
            )

        [1] => Array
            (
                [themeID] => 2
                [theme_type] => general
                [theme_file] => logo
                [theme_default] => 1
                [theme_title] => CTT Group
                [title] => CTT Industries
            )

        [2] => Array
            (
                [themeID] => 5
                [theme_type] => general
                [theme_file] => logo
                [theme_default] => 0
                [theme_title] => CTT Logo 2
                [src] => uploads/img/cttlogo2.png
            )

        [3] => Array
            (
                [themeID] => 5
                [theme_type] => general
                [theme_file] => logo
                [theme_default] => 0
                [theme_title] => CTT Logo 2
                [title] => CTF Associates
            )

    )

)

问题是我想按 ID 号列出主题数组,以便重复的字段相互覆盖,仅插入尚未定义的字段。

我正在寻找结果是这样的:

Array
    (
        [contentID] => 9
        [type] => operations
        [title] => CTK Partners
        [parent] => 8
        [theme] => Array
        (
            [themeID2] => Array
                (
                    [themeID] => 2
                    [theme_type] => general
                    [theme_file] => logo
                    [theme_default] => 1
                    [theme_title] => CTT Group
                    [src] => uploads/img/clogo.png
                    [title] => CTT Industries
                )

            [themeID5] => Array
                (
                    [themeID] => 5
                    [theme_type] => general
                    [theme_file] => logo
                    [theme_default] => 0
                    [theme_title] => CTT Logo 2
                    [src] => uploads/img/cttlogo2.png
                    [title] => CTF Associates
                )

        )

)

如何做到这一点?

4

2 回答 2

1

为 中的每一行指定一个键$pagecontent['theme'],如

 $pagecontent['theme']["themeID{$cont['themeID']}"] = $themearray;

代替array_push线。

于 2013-10-27T11:19:52.647 回答
0

丹的回答是正确的,但它没有考虑到循环可能需要添加到主题数组而不覆盖它。他的代码的这种改编考虑到了这一点。

if(!$pagecontent['theme']["themeID{$cont['themeID']}"]){
        $pagecontent['theme']["themeID{$cont['themeID']}"] = $themearray;
    }
    else {
        $pagecontent['theme']["themeID{$cont['themeID']}"][$cont['meta_field']] = $cont['meta_value'];
    }
于 2013-10-27T11:29:45.710 回答