PHP转换多数组

时间:2018-06-21 11:53:24

标签: php multidimensional-array

下面是我在数组中的输出

Array 
(
    [0] => Array (
                    [subjectId] => 13 
                    [subjectName] => Chemistry
                    [chapterId] => 1 
                    [chapterName] => Chem 1 
                ) 
    [1] => Array
                ( 
                    [subjectId] => 13 
                    [subjectName] => Chemistry 
                    [chapterId] => 2
                    [chapterName] => Chem 2 
                ) 

    [2] => Array ( 
                    [subjectId] => 14
                    [subjectName] => Mathematics 
                    [chapterId] => 5 
                    [chapterName] => Math 1 
                )
    [3] => Array ( 
                    [subjectId] => 14 
                    [subjectName] => Mathematics 
                    [chapterId] => 6 
                    [chapterName] => Math 2 
                )
) 

我正在寻找什么

Array
(
[0] => Array
(
    [subjectId] => 13
    [subjectName] =>  Chemistry
    [chapters] => Array
                        (
                            [0] => Array
                                (
                                    [chapterId] => 1
                                    [chapterName] => Chem 1
                                )
                            [1] => Array
                                (
                                    [chapterId] => 2
                                    [chapterName] => Chem 2
                                )

                        )

)
[1] => Array
(
    [subjectId] => 14
    [subjectName] => Mathematics
    [chapters] => Array
                    (

                        [0] => Array
                            (

                                [chapterId] => 5
                                [chapterName] => Math 1

                            )

                        [1] => Array
                            (

                                [chapterId] => 6
                                [chapterName] => Math 2

                            )

                    )

    )

)

1 个答案:

答案 0 :(得分:0)

一种选择是使用array_reduce将数组分组为关联数组,并使用subjectId作为键。使用array_values将关联数组转换为简单数组。

$result = array_reduce($arr, function($c, $v){
    if ( !isset( $c[$v['subjectId']] ) ) $c[$v['subjectId']] = array( 'subjectId'=> $v['subjectId'], 'subjectName' => $v['subjectName'], 'chapters' => array() );
    $c[$v['subjectId']]['chapters'][] = array( 'chapterId' => $v['chapterId'], 'chapterName' => $v['chapterName']);

    return $c;
}, array());

$result = array_values( $result );

这将导致:

Array
(
    [0] => Array
        (
            [subjectId] => 13
            [subjectName] => Chemistry
            [chapters] => Array
                (
                    [0] => Array
                        (
                            [chapterId] => 1
                            [chapterName] => Chem 1
                        )

                    [1] => Array
                        (
                            [chapterId] => 2
                            [chapterName] => Chem 2
                        )

                )

        )

    [1] => Array
        (
            [subjectId] => 14
            [subjectName] => Mathematics
            [chapters] => Array
                (
                    [0] => Array
                        (
                            [chapterId] => 5
                            [chapterName] => Math 1
                        )

                    [1] => Array
                        (
                            [chapterId] => 6
                            [chapterName] => Math 2
                        )

                )

        )

)