没有多个嵌套父ID的平面数组

时间:2015-03-12 05:33:36

标签: php arrays

使用多种类型的“parentid”从平面数组创建树或嵌套数组 来自:

courseid,  activityid, userid 

course1 , activity2 , user 3 
course 1 , activity2 , user 4 
course 1 , activity3 , user 3
course 2 , activity 1 , user 2  

为:

course1
    activity2
        user 3
        user 4
    activity 3
        user 3
course3
    activity 1
        user 2

1 个答案:

答案 0 :(得分:0)

$rFile = fopen( 'infile4.csv', 'r' );
$iLineNumber = 0;
while( !feof( $rFile ) )
{
    if( $iLineNumber == 0 )
    {
        $aHeaders = fgetcsv( $rFile );
    }
    else
    {
        $aTmp = array();
        $aTmp = fgetcsv( $rFile );

        $aFormatTmp[ $aHeaders[ 0 ] ] = trim( $aTmp[ 0 ] );
        $aFormatTmp[ $aHeaders[ 1 ] ] = trim( $aTmp[ 1 ] );
        $aFormatTmp[ $aHeaders[ 2 ] ] = trim( $aTmp[ 2 ] );
        $aFormatted[] = $aFormatTmp;
    }


    ++$iLineNumber;
}
var_dump( $aFormatted );