【问题标题】:Structure mySQL results into multidimensional array将 mySQL 结果结构化为多维数组
【发布时间】:2013-10-27 11:08:21
【问题描述】:

以下代码从 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
                )

        )

)

如何做到这一点?

【问题讨论】:

    标签: php loops multidimensional-array foreach


    【解决方案1】:

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

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

    代替array_push 行。

    【讨论】:

    • 非常好。它虽然没有考虑到可能需要添加一个额外的字段。我修改了您的代码,因此它也可以用于此目的,因此加一票。我的答案在下面使用您的代码。它似乎可以工作,但是如果您发现问题,请告诉我。谢谢,
    【解决方案2】:

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

    if(!$pagecontent['theme']["themeID{$cont['themeID']}"]){
            $pagecontent['theme']["themeID{$cont['themeID']}"] = $themearray;
        }
        else {
            $pagecontent['theme']["themeID{$cont['themeID']}"][$cont['meta_field']] = $cont['meta_value'];
        }
    

    【讨论】:

      猜你喜欢
      • 2014-12-04
      • 2011-08-28
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多