简体   繁体   English

PHP从数组创建数组

[英]PHP create arrays from array

I have two tables, content and theme . 我有两个表, contenttheme There are several theme rows that should be obtained for each row in the content table. 对于content表中的每一行,应该获得几个theme行。 This I have done with PDO and a mySQL query involving a LEFT JOIN . 我使用PDO和涉及LEFT JOIN的mySQL查询完成了此操作。 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. 我想重新组织数组,以便每个contentID获得一个数组,该数组中的数组包含每个匹配的主题文件。

The idea being that something like $content=>'theme'['logo'] would grab the value for one of the matching theme files. 这样的想法是,像$content=>'theme'['logo']这样的东西将获取匹配主题文件之一的值。

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: 实际上,我希望相互嵌套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 

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 因此,目的是让每个content行嵌套所有匹配的theme行,并让每个theme行嵌套每个匹配的theme_meta

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

)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM