0

I have two tables, content and theme. There are several theme rows that should be obtained for each row in the content table. This I have done with PDO and a mySQL query involving a LEFT JOIN. The output array gives me one array per occurence of a theme file (below).

I want to reorganise the array so that I get one array per contentID with an array within that array containing each of the matching theme files.

The idea being that something like $content=>'theme'['logo'] would grab the value for one of the matching theme files.

How would this be done.

array(
       array (
            contentID => 1, 
            title => 'test', 
            subtitle = 'a description', 
            themeID => 1,
            theme_file => 'navigation'
        )

        array (
            contentID => 1, 
            title => 'test', 
            subtitle = 'a description', 
            themeID => 2,
            theme_file => 'logo'
        )
)

As requested I have added additional code. There are in fact 3 tables I wish to nest inside each other but for the purpose of a helpful question to others I kept it at 2. This is the mySQL query if helpful:

SELECT * 
FROM content
LEFT JOIN content_theme ON content.contentID = content_theme.contentID
LEFT JOIN theme ON theme.themeID = content_theme.themeID
OR theme.default =1
LEFT JOIN theme_meta ON theme.themeID = theme_meta.themeID
WHERE content.contentID = 9
ORDER BY theme.default DESC 

The intention therefore is to for each content row to nest all its matching theme rows, and for each of these theme rows to nest each of their matching theme_meta rows

Raw output from the database:

Array
(
    [0] => Array
        (
            [contentID] => 
            [0] => 4
            [type] => page
            [1] => page
            [type_alts] => 
            [2] => 
            [url] => about
            [3] => about
            [title] => About
            [4] => About
            [subtitle] => 
            [5] => 
            [online] => 1
            [6] => 1
            [req] => 0
            [7] => 0
            [pos] => 0
            [8] => 0
            [parent] => 0
            [9] => 0
            [content_theme_ID] => 
            [10] => 
            [11] => 
            [themeID] => 2
            [12] => 
            [13] => 2
            [theme_type] => general
            [14] => general
            [theme_file] => logo
            [15] => logo
            [theme_default] => 1
            [16] => 1
            [theme_title] => MD Group
            [17] => MD Group
            [18] => 2
            [field] => src
            [19] => src
            [value] => uploads/img/murphey-dines-group-logo.png
            [20] => uploads/img/murphey-dines-group-logo.png
        )

    [1] => Array
        (
            [contentID] => 
            [0] => 4
            [type] => page
            [1] => page
            [type_alts] => 
            [2] => 
            [url] => about
            [3] => about
            [title] => About
            [4] => About
            [subtitle] => 
            [5] => 
            [online] => 1
            [6] => 1
            [req] => 0
            [7] => 0
            [pos] => 0
            [8] => 0
            [parent] => 0
            [9] => 0
            [content_theme_ID] => 
            [10] => 
            [11] => 
            [themeID] => 2
            [12] => 
            [13] => 2
            [theme_type] => general
            [14] => general
            [theme_file] => logo
            [15] => logo
            [theme_default] => 1
            [16] => 1
            [theme_title] => MD Group
            [17] => MD Group
            [18] => 2
            [field] => title
            [19] => title
            [value] => murphey dines Group
            [20] => murphey dines Group
        )

    [2] => Array
        (
            [contentID] => 
            [0] => 4
            [type] => page
            [1] => page
            [type_alts] => 
            [2] => 
            [url] => about
            [3] => about
            [title] => About
            [4] => About
            [subtitle] => 
            [5] => 
            [online] => 1
            [6] => 1
            [req] => 0
            [7] => 0
            [pos] => 0
            [8] => 0
            [parent] => 0
            [9] => 0
            [content_theme_ID] => 
            [10] => 
            [11] => 
            [themeID] => 
            [12] => 
            [13] => 7
            [theme_type] => general
            [14] => general
            [theme_file] => navigation
            [15] => navigation
            [theme_default] => 1
            [16] => 1
            [theme_title] => Main Navigation
            [17] => Main Navigation
            [18] => 
            [field] => 
            [19] => 
            [value] => 
            [20] => 
        )

)
4
  • PHP is pretty good at making nested arrays the way you described. But you probably may need to share more code to get help with what you're asking. Commented Oct 20, 2013 at 16:36
  • @JoeT additional code added. Commented Oct 20, 2013 at 16:40
  • Could you show an example result set from your query? I think you basically want to loop over all the ContentIDs, then within that loop run your query, get the results as an associative array and loop over that assigning $content[$ContentID][$theme] equal to each row. Commented Oct 20, 2013 at 16:50
  • @JoeT that is exactly what I want to do. I'll add the array output. Commented Oct 20, 2013 at 16:54

1 Answer 1

0

You can nest the arrays like this i suppose.

$aContent = array();
foreach($aRows AS $aRow){
    $aContent[$aRow['contentID']][] = array(
        'theme' => array(
            'themeID' => $aRow['themeID'],
            'themeType' => $aRow['theme_type'],
            'themeTitle' => $aRow['theme_title']
        ),
        'type' => 'page',
    );
}

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.