Walrus
Walrus

Reputation: 20444

Structure mySQL results into multidimensional array

The following code take an array containing a series of row from a mySQL JOIN and restructures them into a multidimensional array.

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

}

The output array printed is below.

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
            )

    )

)

The problem is that I want to list the theme arrays by ID number so that fields that repeat write over each other inserting only fields that are not already defined.

I am looking for the outcome to be this:

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
                )

        )

)

How can this be achieved?

Upvotes: 0

Views: 61

Answers (2)

Walrus
Walrus

Reputation: 20444

Dan's answer is correct but it doesn't take into account that loops may need to add to the theme array without overwriting it. This adaption of his code takes this into account.

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

Upvotes: 0

Dan
Dan

Reputation: 4502

Specify a key for each row in $pagecontent['theme'], as in

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

in place of the array_push line.

Upvotes: 1

Related Questions