麻辣堂|资源主站|开发论坛|在线手册
首页 Apache Linux Java MySQL 注册帮助 
PHP项目开发组是PHP开发资源网于2007组年建成立的项目开发团队,目前核心开发成员有27人, 项目协作成员8名.下设7个开发组,主要承接大/中型网站项目开发任务。

    由于开发任务较多,人员比较紧张,现面向社会招聘全职或者兼职开发人员,不管你是在校大学生,还是全职开发人员,以及SOHO都可以联系本站,我们可以长期合作,并为您带来丰厚的报酬。
  您现在的位置:PHP开发资源网 > 麻辣堂 > 详细资料
待解决
array to php 的Class
悬赏分:20 - 2007年08月22日

[php]
<?php
class PHPArrayDumper
{
/**
* Given a PHP array, output a string which, when eval'd, reproduces the array.
*
* Recursive algorithm. Doens't support objects or resources.
*
* @param array The array to dump.
* @return string PHP code for the array in format "array(...);".
* @throws Exception if an unsupported type is encountered.
*/
function arrayToPHPSource($array, $indent = 0)
{
$arrayStr = '';
$indentTpl = " ";
$indentStr = str_repeat($indentTpl, $indent + 1);

foreach ($array as $key => $value) {
switch (gettype($value)) {
case 'integer':
case 'float':
case 'double':
$arrayStr .= "$indentStr'$key' => $value,\n";
break;
case 'NULL':
$arrayStr .= "$indentStr'$key' => NULL,\n";
break;
case 'boolean':
$arrayStr .= "$indentStr'$key' => " . ((boolean) $value ? 'true' : 'false') . ",\n";
break;
case 'string':
$arrayStr .= "$indentStr'$key' => '$value',\n";
break;
case 'array':
$arrayStr .= "$indentStr'$key' => " . PHPArrayDumper::arrayToPHPSource($value, $indent + 1) . ",\n";
break;
default:
throw( new Exception("Unsupported value type: " . gettype($value) . " encountered.") );
break;
}
}

if ($indent == 0)
{
$arrayStr = "array(\n" . $arrayStr . ');';
return $arrayStr;
}
else
{
$arrayStr = "array(\n" . $arrayStr . str_repeat($indentTpl, $indent) . ')';
return $arrayStr;
}
}

/**
* Similar to {@link arrayToPHPSource} but gives results in "$myVarName = array(...);" format.
*
* @param array The array to dump.
* @return string PHP code for the array in format "$myVarName = array(...);".
* @throws Exception if an unsupported type is encountered.
*/
function arrayToPHPVariableSource($array, $varName)
{
return "\${$varName} = " . PHPArrayDumper::arrayToPHPSource($array);
}

function arrayToPHPFileWithArray($array, $varName, $filePath)
{
$source = "<?php\n" . PHPArrayDumper::arrayToPHPVariableSource($array, $varName) . "\n?>\n";
file_put_contents($filePath, $source);
}
}
?>
[/php]

提问者:菜鸟也要有理想   08-22 14:02
答复
路过。。。顺便帮顶:)
回答者:玉米づ冰冻可乐 - 瓦岗村民 8-22 09:10
我也来回答:
不管你有没有帮助我们,瓦岗寨8万村民将感谢你。。。。。

为防止灌水,您需要计算一道数学题: 答案:
47 + 51 = ? 请将计算结果填在上面

 
[]
©2007 PhpRes.COM