doufuxi7093 2018-09-27 18:05
浏览 48
已采纳

重新格式化PHP MYSQL阵列结果深入多个级别

I reformatted this question hoping that the duplicate marking of this question will be changed as that answer doesn't go as far as this need does. It is not clear how to get past the first level of SECTIONS containing all CHAPTERS which the other question explains how to do.

I'm trying to first group by SECTION, then by all the CHAPTERS in that section and then all the PAGES within each chapter. Hope someone can help me.

Here is the result I get back from MYSQL in PHP:

Array
(
    [0] => Array
        (
            [PageId] => 1
            [BookSection] => Section 1
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 1
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 1
            [PageAudioUrl] => http://cloudfront.net/mp3/books/1.mp3
        )

    [1] => Array
        (
            [PageId] => 2
            [BookSection] => Section 1
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 2
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 1
            [PageAudioUrl] => http://cloudfront.net/mp3/books/2.mp3
        )

    [2] => Array
        (
            [PageId] => 3
            [BookSection] => Section 1
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 3
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 1
            [PageAudioUrl] => http://cloudfront.net/mp3/books/3.mp3
        )

    [3] => Array
        (
            [PageId] => 4
            [BookSection] => Section 1
            [ChapterNumber] => 2
            [ChapterTitle] => Chapter 2
            [PageNumber] => 4
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/4.mp3
        )

    [4] => Array
        (
            [PageId] => 5
            [BookSection] => Section 1
            [ChapterNumber] => 2
            [ChapterTitle] => Chapter 2
            [PageNumber] => 5
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/5.mp3
        )

    [5] => Array
        (
            [PageId] => 6
            [BookSection] => Section 2
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 6
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/6.mp3
        )

    [6] => Array
        (
            [PageId] => 7
            [BookSection] => Section 2
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 7
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/7.mp3
        )

    [7] => Array
        (
            [PageId] => 8
            [BookSection] => Section 2
            [ChapterNumber] => 2
            [ChapterTitle] => Chapter 2
            [PageNumber] => 8
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/8.mp3
        )

    [8] => Array
        (
            [PageId] => 9
            [BookSection] => Section 2
            [ChapterNumber] => 2
            [ChapterTitle] => Chapter 2
            [PageNumber] => 9
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/9.mp3
        )

    [9] => Array
        (
            [PageId] => 10
            [BookSection] => Section 2
            [ChapterNumber] => 2
            [ChapterTitle] => Chapter 2
            [PageNumber] => 10
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/10.mp3
        )

    [10] => Array
        (
            [PageId] => 11
            [BookSection] => Section 3
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 11
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/11.mp3
        )

    [11] => Array
        (
            [PageId] => 12
            [BookSection] => Section 3
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 12
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/12.mp3
        )

    [12] => Array
        (
            [PageId] => 13
            [BookSection] => Section 3
            [ChapterNumber] => 1
            [ChapterTitle] => Chapter 1
            [PageNumber] => 13
            [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


            [PageIsFree] => 0
            [PageAudioUrl] => http://cloudfront.net/mp3/books/13.mp3
        )

)

I'm trying to format the array to look like this:

[0] => Array
    (
        [SectionName] => Section 1
        [Chapters] => Array
            (
                [0] => Array
                   (                
                        [ChapterTitle] => Chapter 1
                        [ChapterNumber] => 1
                        [Pages] => Array 
                            (
                                [0] => Array
                                   (                
                                        [PageId] => 1
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 1
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/1.mp3
                                    }   
                                [1] => Array
                                   (                
                                        [PageId] => 2
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 2
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/2.mp3
                                    }   
                                [2] => Array
                                   (                
                                        [PageId] => 3
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 3
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/3.mp3
                                    )
                            )       
                    )
                [1] => Array
                   (                
                        [ChapterTitle] => Chapter 2
                        [ChapterNumber] => 2
                        [Pages] => Array 
                            (
                                [0] => Array
                                   (                
                                        [PageId] => 4
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 4
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/4.mp3
                                    }   
                                [1] => Array
                                   (                
                                        [PageId] => 5
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 5
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/5.mp3
                                    }   
                                [2] => Array
                                   (                
                                        [PageId] => 6
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 6
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/6.mp3
                                    )
                            )       
                    )
    )                   
[1] => Array
    (
        [SectionName] => Section 2
        [Chapters] => Array
            (
                [0] => Array
                   (                
                        [ChapterTitle] => Chapter 1
                        [ChapterNumber] => 1
                        [Pages] => Array 
                            (
                                [0] => Array
                                   (                
                                        [PageId] => 7
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 1
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/7.mp3
                                    }   
                                [1] => Array
                                   (                
                                        [PageId] => 8
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 2
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/8.mp3
                                    }   
                                [2] => Array
                                   (                
                                        [PageId] => 9
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 3
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/9.mp3
                                    )
                            )       
                    )
                [1] => Array
                   (                
                        [ChapterTitle] => Chapter 2
                        [ChapterNumber] => 2
                        [Pages] => Array 
                            (
                                [0] => Array
                                   (                
                                        [PageId] => 10
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 4
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/10.mp3
                                    }   
                                [1] => Array
                                   (                
                                        [PageId] => 11
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 5
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/11.mp3
                                    }   
                                [2] => Array
                                   (                
                                        [PageId] => 12
                                        [PageText] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
                                        [PageNumber] => 6
                                        [PageIsFree] => 0
                                        [PageAudioUrl] => http://cloudfront.net/mp3/books/12.mp3
                                    )
                            )       
                    )
    )       

I have attempted several solutions, this is the closest I get, but the groupings are separated in the results:

    foreach ($book_content as $record) {

        $temp_array['SectionName'][] = $record['BookSection'];
        $temp_array['Chapters'][] = array('ChapterTitle' => $record['ChapterTitle']);
        $temp_array['Chapters']['Pages'][] = $record;
    }

echo '<pre>'; print_r($temp_array);
die();

Which gives me:

Array
(
    [0] => Array
        (
            [SectionName] => Array
                (
                    [0] => Section 1
                    [1] => Section 1
                    [2] => Section 1
                    [3] => Section 1
                    [4] => Section 1
                    [5] => Section 2
                    [6] => Section 2
                    [7] => Section 2
                    [8] => Section 2
                    [9] => Section 2
                    [10] => Section 3
                    [11] => Section 3
                    [12] => Section 3
                )

            [Chapters] => Array
                (
                    [0] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [1] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [2] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [3] => Array
                        (
                            [ChapterTitle] => Chapter 2
                        )

                    [4] => Array
                        (
                            [ChapterTitle] => Chapter 2
                        )

                    [5] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [6] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [7] => Array
                        (
                            [ChapterTitle] => Chapter 2
                        )

                    [8] => Array
                        (
                            [ChapterTitle] => Chapter 2
                        )

                    [9] => Array
                        (
                            [ChapterTitle] => Chapter 2
                        )

                    [10] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [11] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                    [12] => Array
                        (
                            [ChapterTitle] => Chapter 1
                        )

                )


                    [Pages] => Array
                        (
                            [0] => Array
                                (
                                    [PageId] => 1
                                    [BookSection] => Section 1
                                    [ChapterNumber] => 1
                                    [ChapterTitle] => Chapter 1
                                    [PageNumber] => 1
                                    [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


                                    [PageIsFree] => 1
                                    [PageAudioUrl] => http://d2q6xzim332bmh.cloudfront.net/mp3/books/1.mp3
                                )

                            [1] => Array
                                (
                                    [PageId] => 2
                                    [BookSection] => Section 1
                                    [ChapterNumber] => 1
                                    [ChapterTitle] => Chapter 1
                                    [PageNumber] => 2
                                    [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


                                    [PageIsFree] => 1
                                    [PageAudioUrl] => http://d2q6xzim332bmh.cloudfront.net/mp3/books/2.mp3
                                )

                            [2] => Array
                                (
                                    [PageId] => 3
                                    [BookSection] => Section 1
                                    [ChapterNumber] => 1
                                    [ChapterTitle] => Chapter 1
                                    [PageNumber] => 3
                                    [PageText] => 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum enim ac massa feugiat aliquam. Curabitur molestie sit amet ipsum sed accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 


                                    [PageIsFree] => 1
                                    [PageAudioUrl] => http://d2q6xzim332bmh.cloudfront.net/mp3/books/3.mp3
                                )
                )

        )

A second attempt gets me the right structure, but the like-values are not grouped. In other words, each time a page is included in Section 1 > Chapter 1 it shows as unique array instead of grouped within the Section 1 > Chapter 1 array.

$temp_array = array();
$i = 0;
foreach ($book_content as $record) {

    $temp_array[$i]['SectionName'] = $record['BookSection'];
    $temp_array[$i]['Chapters'][$i]['ChapterTitle'] = $record['ChapterTitle'];
    $temp_array[$i]['Chapters'][$i]['Pages'][$i] = $record;
    $i++;
}
  • 写回答

1条回答 默认 最新

  • dongsu7049 2018-10-01 19:33
    关注

    You need to use associative arrays that are keyed off the section names and chapter numbers, so you can group these related rows together. You're just adding a new element in the result for each element in the original array, which doesn't do any regrouping.

    You go through the original array, checking whether there's already an entry for the section or chapter already in the result. If not, you add an initial entry for it, then fill it in with the details.

    $result = array();
    foreach ($book_content as $record) {
        $section = $record['BookSection'];
        if (!isset($result[$section])) {
            // Create initial entry for this section
            $result[$section] = array(
                'SectionName' => $section,
                'Chapters' => array());
        }
        $chapter = $record['ChapterNumber']
        if (!isset($result[$section]['Chapters'][$chapter])) {
            // Create initial entry for this chapter
            $result[$section]['Chapters'][$chapter] = array(
                'ChapterNumber' => $chapter,
                'ChapterTitle' => $record['ChapterTitle'],
                'Pages' => array());
        }
        // Add record for this page
        $result[$section]['Chapters'][$chapter]['Pages'][] = array(
            'PageID' => $record['PageID'],
            'PageText' => $record['PageText'],
            'PageNumber' => $record['PageNumber'],
            'PageIsFree' => $record['PageIsFree'],
            'PageAudioUrl' => $record['PageAudioUrl']));
    }
    

    The result of this will be an associative array rather than index. If you need it to be indexed, you can use array_values():

    $result = array_values($result);
    foreach ($result as &$section) {
        $section['Chapters'] = array_values($section['Chapters'];
    }
    unset($section); // Break the reference variable link
    

    Don't forget the & in the foreach, which makes $section a reference, so that assigning to this affects the array you're iterating over.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?