0

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?

2 Answers 2

1

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

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

in place of the array_push line.

Sign up to request clarification or add additional context in comments.

1 Comment

Very good. It doesn't though take into account that an extra field may need to be added. I modified your code so it would work for this purpose as well, hence the plus 1 vote. My answer is below using your code. It seams to work but if you see a problem, do let me know. Thanks,
0

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'];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.