關(guān)于PHP遞歸數(shù)組代碼分析
我們大家都知道PHP是一種HTML內(nèi)嵌式的語(yǔ)言,PHP與微軟的ASP頗有幾分相似,都是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語(yǔ)言,語(yǔ)言的風(fēng)格有類似于C語(yǔ)言,現(xiàn)在被很多的網(wǎng)站編程人員廣泛的運(yùn)用。文章這里詳細(xì)的介紹一下PHP遞歸數(shù)組。PHP程序需要將接收到的數(shù)據(jù)同時(shí)寫(xiě)到“線上運(yùn)行的正式數(shù)據(jù)庫(kù)”和“進(jìn)行開(kāi)發(fā)調(diào)試的測(cè)試數(shù)據(jù)庫(kù)”。
#T#而測(cè)試數(shù)據(jù)庫(kù)可能經(jīng)常會(huì)面臨對(duì)表結(jié)構(gòu)、字段、配置信息做調(diào)整等問(wèn)題,很不穩(wěn)定,發(fā)生錯(cuò)誤的概率很高,如果用a.php程序同時(shí)寫(xiě)“正式數(shù)據(jù)庫(kù)”和 “測(cè)試數(shù)據(jù)庫(kù)”,勢(shì)必影響到線上運(yùn)行的正式服務(wù)。于是,我想到用PHP curl擴(kuò)展庫(kù)將生成的$data數(shù)組post傳遞一份給php程序,然后php程序繼續(xù)往下執(zhí)行寫(xiě)“正式數(shù)據(jù)庫(kù)”的代碼。php程序?qū)?data數(shù)組傳遞給php程序就完事了,至于php如何處理,就不關(guān)php的事了,php程序即使寫(xiě)“測(cè)試數(shù)據(jù)庫(kù)”失敗,也不會(huì)對(duì) php程序造成影響。
PHP遞歸數(shù)組源代碼:
- <?php
- $data["username"]="張宴";
- $data["password"]="不知道";
- $data["ip"]="192.168.0.18";
- //reGISter_shutdown_function("post_data", $data);
- //function post_data($data)
- //{
- $curl = new Curl_Class();
- $post = @$curl->post("http://127.0.0.1/b.php", $data);//這里是b.php的訪問(wèn)地址,請(qǐng)自行修改
- //}
- //curl類
- class Curl_Class
- {
- function Curl_Class()
- {
- return true;
- }
- function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ch = Curl_Class::create();
- if (false === $ch)
- {
- return false;
- }
- if (is_string($url) && strlen($url))
- {
- $ret = curl_setopt($ch, CURLOPT_URL, $url);
- }
- else
- {
- return false;
- }
- //是否顯示頭部信息
- curl_setopt($ch, CURLOPT_HEADER, false);
- //
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($username != '')
- {
- curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
- }
- $method = strtolower($method);
- if ('post' == $method)
- {
- curl_setopt($ch, CURLOPT_POST, true);
- if (is_array($fields))
- {
- $sets = array();
- foreach ($fields AS $key => $val)
- {
- $sets[] = $key . '=' . urlencode($val);
- }
- $fields = implode('&',$sets);
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
- }
- else if ('put' == $method)
- {
- curl_setopt($ch, CURLOPT_PUT, true);
- }
- //curl_setopt($ch, CURLOPT_PROGRESS, true);
- //curl_setopt($ch, CURLOPT_VERBOSE, true);
- //curl_setopt($ch, CURLOPT_MUTE, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);//設(shè)置curl超時(shí)秒數(shù),例如將信息POST出去3秒鐘后自動(dòng)結(jié)束運(yùn)行。
- if (strlen($userAgent))
- {
- curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
- }
- if (is_array($httpHeaders))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
- }
- $ret = curl_exec($ch);
- if (curl_errno($ch))
- {
- curl_close($ch);
- return array(curl_error($ch), curl_errno($ch));
- }
- else
- {
- curl_close($ch);
- if (!is_string($ret) || !strlen($ret))
- {
- return false;
- }
- return $ret;
- }
- }
- function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
- if (false === $ret)
- {
- return false;
- }
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
- function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '')
- {
- $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
- if (false === $ret)
- {
- return false;
- }
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
- function create()
- {
- $ch = null;
- if (!function_exists('curl_init'))
- {
- return false;
- }
- $ch = curl_init();
- if (!is_resource($ch))
- {
- return false;
- }
- return $ch;
- }
- }
- ?>
PHP遞歸數(shù)組代碼:
- <?php
- ignore_user_abort();//連線中斷后(例如關(guān)閉瀏覽器)仍然繼續(xù)執(zhí)行以下的腳本直到處理完畢。
- set_time_limit(0);
- $get_data = file_get_contents("php://input");
- $explodeexplodedata = explode("&", $get_data);
- foreach ($explodedata as $key => $value)//還原數(shù)組
- {
- list($realkey, $realvalue) = explode("=", $value);
- $data[urldecode($realkey)] = urldecode($realvalue);
- }
- //現(xiàn)在$data數(shù)組已經(jīng)和a.php中的一樣了,接下來(lái),就可以根據(jù)自己的需要對(duì)$data數(shù)組進(jìn)行操作了。
- //......
- ?>