PHP从数组创建数组

PHP从数组创建数组,php,arrays,multidimensional-array,pdo,Php,Arrays,Multidimensional Array,Pdo,我有两个表,content和theme。对于内容表中的每一行,都应该获得多个主题行。这是我用PDO和一个mySQL查询完成的,它涉及一个左连接。输出数组为每次出现主题文件(如下)提供一个数组 我想重新组织数组,以便每个contentID获得一个数组,该数组中包含每个匹配的主题文件 这样做的想法是,像$content=>'theme'['logo']这样的东西将获取其中一个匹配主题文件的值 如何做到这一点 array( array ( contentID =&

我有两个表,
content
theme
。对于
内容
表中的每一行,都应该获得多个
主题
行。这是我用PDO和一个mySQL查询完成的,它涉及一个
左连接。输出数组为每次出现主题文件(如下)提供一个数组

我想重新组织数组,以便每个
contentID
获得一个数组,该数组中包含每个匹配的主题文件

这样做的想法是,像
$content=>'theme'['logo']
这样的东西将获取其中一个匹配主题文件的值

如何做到这一点

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'
        )
)
根据要求,我添加了额外的代码。事实上,我希望将3张桌子相互嵌套,但为了向其他人提出一个有用的问题,我将其保留为2张。这是mySQL查询(如果有帮助):

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 
因此,目的是为每个
内容
行嵌套其所有匹配的
主题
行,并为每个
主题
行嵌套其每个匹配的
主题

数据库的原始输出:

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] => 
        )

)

我想你可以像这样嵌套数组

$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',
    );
}

PHP非常擅长按照您描述的方式创建嵌套数组。但您可能需要共享更多的代码,以获得对您所请求内容的帮助。@JoeT添加了其他代码。能否显示查询结果集的示例?我认为您基本上希望循环所有ContentID,然后在该循环中运行查询,以关联数组的形式获取结果,并循环指定$content[$ContentID][$theme]等于每一行。@JoeT这正是我想要做的。我将添加数组输出。