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
    );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM