首页 > 代码库 > php 显示php 中数组return Array
php 显示php 中数组return Array
php 读取文本中数组,修改指定数组键值。达到统计下载文件排行。
<?php
header("Content-type:text/html;charset=utf-8");
if(isset($_GET[‘down_id‘]) && !empty($_GET[‘down_id‘])){
$down_id = $_GET[‘down_id‘];
$file=‘down_id.php‘;
$du = require_once($file);
arsort($du);
if (!isset($du[$down_id])){
$du[$down_id]= ‘1‘;
}else{
$du[$down_id]=$du[$down_id]+1;
}
cache_write($file,$du,‘rows‘,false);
Header("Location:$down_id");
}else{
echo ‘不存在!‘;
}
//写入函数
function cache_write($filename,$values,$var=‘rows‘,$format=false){
$cachefile=$filename;
$cachetext="<?php\r\n".‘return ‘.arrayeval($values,$format).";";
return writefile($cachefile,$cachetext);
}
//数组转换成字串函数
function arrayeval($array,$format=false,$level=0){
$space=$line=‘‘;
if(!$format){
for($i=0;$i<=$level;$i++){
$space.="\t";
}
$line="\n";
}
$evaluate=‘Array (‘.‘ //自动排序‘.$line;
$comma=$space;
foreach($array as $key=> $val){
$key=is_string($key)?‘\‘‘.addcslashes($key,‘\‘\\‘).‘\‘‘:$key;
$val=!is_array($val)&&(!preg_match(‘/^\-?\d+$/‘,$val)||strlen($val) > 12)?‘\‘‘.addcslashes($val,‘\‘\\‘).‘\‘‘:$val;
if(is_array($val)){
$evaluate.=$comma.$key.‘ => ‘.arrayeval($val,$format,$level+1);
}else{
$evaluate.=$comma.$key.‘ => ‘.$val;
}
$comma=‘,‘.$line.$space;
}
$evaluate.=$line.$space.‘)‘;
return $evaluate;
}
//写入文件函数
function writefile($filename,$writetext,$openmod=‘w‘){
if(false!==$fp=fopen($filename,$openmod)){
flock($fp,2);
fwrite($fp,$writetext);
fclose($fp);
return true;
}else{
return false;
}
}
效果为
<?php return Array ( //自动排序 ‘2345explorer.zip‘ => 21, ‘2345pcsafe.zip‘ => 13, ‘360sd_114448.exe‘ => 3, ‘u.exe‘ => 2, ‘tel.zip‘ => 2, ‘Adobe Flash Player ActiveX_23.0.0.185.exe‘ => 1, ‘360sd_x64_std_5.0.0.7033.exe‘ => 1, ‘360se 114448.exe‘ => 1, ‘360safe 114448.exe‘ => 1, ‘desktop.zip‘ => 1, ‘360sd_std_5.0.0.7033.exe‘ => 1, ‘Adobe Flash Player_NPAPI_23.0.0.185.exe‘ => 1, ‘u1603.exe‘ => 1 );
php 显示php 中数组return Array