Home > php教程 > php手册 > body text

关于php散列数组与格式化(zendframework中的ini读取方式)

WBOY
Release: 2016-06-06 19:47:02
Original
997 people have browsed it

时间有限,记录一个列子 $Arr_Test[ 'test.mytest.youtest' ] = 1; 散列结果将'test.mytest.youtest'散列为以test等为键的多维数组 最终的效果是 $Arr_Test[ 'test' ][ 'mytest' ][ 'youtest' ] = 1; /** * @desc 格式化数组 * @param array $Arr_Format * @

  时间有限,记录一个列子 $Arr_Test[ 'test.mytest.youtest' ] = 1;

  散列结果将'test.mytest.youtest'散列为以test等为键的多维数组

  最终的效果是 $Arr_Test[ 'test' ][ 'mytest' ][ 'youtest' ] = 1;

  

/**
 * @desc 格式化数组
 * @param array $Arr_Format
 * @return array
 */
function formatArray( &$Arr_Format )
{
	foreach ( $Arr_Format as $key => $val )
	{
		$Arr_Key = explode( '.', $key );
        if ( is_array( $val ) ) formatArray( $Arr_Format[ $key ] );
		if( 1 = 0; $i-- )
    {
    	 if( $i == $iCountArr - 1 )
    	 {
    	 	$Arr_Tmp[ $Arr_Hash[ $i ] ] = false == $mLastVal ? array() : $mLastVal;
    	 }
    	 else 
    	 {
    	 	$Arr_Tmp[ $Arr_Hash[ $i ] ] = $Arr_Tmp;
    	 	array_shift( $Arr_Tmp );
    	 }
    }
    return $Arr_Tmp;
}
Copy after login

  

  使用formatArray( $Arr_Test );就能实现类型zend散列多维数组的方法。

  附,载入ini文件的方法

  

/**
 * @desc 载入ini文件
 * @param string $cFileName ini file name
 * @param bool $bToArray load mode( if to array, default to object )
 * @return mixed
 */
function loadIniFile( $cFileName, $bToArray = false )
{
	$Arr_IniFile = parse_ini_file( $cFileName, true );
	if( false !== $Arr_IniFile )
	{
		$Arr_IniFile = formatArray( $Arr_IniFile );
		if ( false === $bToArray ) $Arr_IniFile = arrayToObject( $Arr_IniFile );
	}
	return $Arr_IniFile;
}
/**
 * @desc 数组转换为对象
 * @param mixed $e
 * @return mixed
 */
function arrayToObject($e)
{
    if ( 'array' != gettype( $e ) ) return;
    foreach($e as $k=>$v)
    {
        if( gettype($v)=='array' || getType($v)=='object' )
            $e[$k]=(object)arrayToObject($v);
    }
    return (object)$e;
}

/**
 * @desc 对象转换为数组
 * @param mixed $e
 * @return mixed
 */
 function objectToArray($e){
    $e=(array)$e;
    foreach($e as $k=>$v)
    {
        if( gettype($v)=='resource' ) return;
        if( gettype($v)=='object' || gettype($v)=='array' )
            $e[$k]=(array)objectToArray($v);
    }
    return $e;
}
Copy after login

  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!