search
Homephp教程php手册关于php散列数组与格式化(zendframework中的ini读取方式)

时间有限,记录一个列子 $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;
}

  

  使用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;
}

  

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools