Tetris-ing an array

后端 未结 16 1396
时光取名叫无心
时光取名叫无心 2021-01-30 15:38

Consider the following array:

/www/htdocs/1/sites/lib/abcdedd
/www/htdocs/1/sites/conf/xyz
/www/htdocs/1/sites/conf/abc/         


        
16条回答
  •  野的像风
    2021-01-30 15:57

    $arrMain = array(
                '/www/htdocs/1/sites/lib/abcdedd',
                '/www/htdocs/1/sites/conf/xyz',
                '/www/htdocs/1/sites/conf/abc/def',
                '/www/htdocs/1/sites/htdocs/xyz',
                '/www/htdocs/1/sites/lib2/abcdedd'
    );
    function explodePath( $strPath ){ 
        return explode("/", $strPath);
    }
    
    function removePath( $strPath)
    {
        global $strCommon;
        return str_replace( $strCommon, '', $strPath );
    }
    $arrExplodedPaths = array_map( 'explodePath', $arrMain ) ;
    
    //Check for common and skip first 1
    $strCommon = '';
    for( $i=1; $i< count( $arrExplodedPaths[0] ); $i++)
    {
        for( $j = 0; $j < count( $arrExplodedPaths); $j++ )
        {
            if( $arrExplodedPaths[0][ $i ] !== $arrExplodedPaths[ $j ][ $i ] )
            {
                break 2;
            } 
        }
        $strCommon .= '/'.$arrExplodedPaths[0][$i];
    }
    print_r( array_map( 'removePath', $arrMain ) );
    

    This works fine... similar to mark baker but uses str_replace

提交回复
热议问题