Heim > Backend-Entwicklung > PHP-Tutorial > 关于PHP递归数组代码分析_PHP教程

关于PHP递归数组代码分析_PHP教程

WBOY
Freigeben: 2016-07-15 13:28:21
Original
1139 Leute haben es durchsucht

我们大家都知道PHP是一种HTML内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。文章这里详细的介绍一下PHP递归数组。PHP程序需要将接收到的数据同时写到“线上运行的正式数据库”和“进行开发调试的测试数据库”。

而测试数据库可能经常会面临对表结构、字段、配置信息做调整等问题,很不稳定,发生错误的概率很高,如果用a.php程序同时写“正式数据库”和 “测试数据库”,势必影响到线上运行的正式服务。于是,我想到用PHP curl扩展库将生成的$data数组post传递一份给php程序,然后php程序继续往下执行写“正式数据库”的代码。php程序将$data数组传递给php程序就完事了,至于php如何处理,就不关php的事了,php程序即使写“测试数据库”失败,也不会对 php程序造成影响。

PHP递归数组源代码:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>$data["username"]="张宴";  </span></li>
<li class="alt"><span>$data["password"]="不知道";  </span></li>
<li class=""><span>$data["ip"]="192.168.0.18";  </span></li>
<li class="alt"><span>//reGISter_shutdown_function("post_data", $data);  </span></li>
<li class=""><span>//function post_data($data)  </span></li>
<li class="alt"><span>//{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">curl</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Curl_Class();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">post</font></span><span> = @$curl-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>post("http://127.0.0.1/b.php", $data);//这里是b.php的访问地址,请自行修改  </span>
</li>
<li class=""><span>//}  </span></li>
<li class="alt"><span>//curl类  </span></li>
<li class=""><span>class Curl_Class  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>function Curl_Class()  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>return true;  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>function execute($method, $url, $</span><span class="attribute"><font color="#ff0000">fields</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">userAgent</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">httpHeaders</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">username</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">password</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ch</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">Curl_Class</font></span><span>::create();  </span>
</li>
<li class="alt">
<span>if (</span><span class="attribute"><font color="#ff0000">false</font></span><span> === $ch)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>if (is_string($url) && strlen($url))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ret</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_setopt</font></span><span>($ch, CURLOPT_URL, $url);  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>else  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>//是否显示头部信息  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_HEADER, false);  </span></li>
<li class="alt"><span>//  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  </span></li>
<li class="alt"><span>if ($username != '')  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">method</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">strtolower</font></span><span>($method);  </span>
</li>
<li class=""><span>if ('post' == $method)  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_POST, true);  </span></li>
<li class="alt"><span>if (is_array($fields))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">sets</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>();  </span>
</li>
<li class="">
<span>foreach ($fields AS $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $val)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>$sets[] = $key . '=' . urlencode($val);  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">fields</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">implode</font></span><span>('&',$sets);  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>else if ('put' == $method)  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_PUT, true);  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>//curl_setopt($ch, CURLOPT_PROGRESS, true);  </span></li>
<li class="alt"><span>//curl_setopt($ch, CURLOPT_VERBOSE, true);  </span></li>
<li class=""><span>//curl_setopt($ch, CURLOPT_MUTE, false);  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_TIMEOUT, 3);//设置curl超时秒数,例如将信息POST出去3秒钟后自动结束运行。  </span></li>
<li class=""><span>if (strlen($userAgent))  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>if (is_array($httpHeaders))  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ret</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_exec</font></span><span>($ch);  </span>
</li>
<li class="alt"><span>if (curl_errno($ch))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>curl_close($ch);  </span></li>
<li class=""><span>return array(curl_error($ch), curl_errno($ch));  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>else  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>curl_close($ch);  </span></li>
<li class="alt"><span>if (!is_string($ret) || !strlen($ret))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>return $ret;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>function post($url, $fields, $</span><span class="attribute"><font color="#ff0000">userAgent</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">httpHeaders</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">username</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">password</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ret</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">Curl_Class</font></span><span>::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);  </span>
</li>
<li class="alt">
<span>if (</span><span class="attribute"><font color="#ff0000">false</font></span><span> === $ret)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>if (is_array($ret))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>return $ret;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span>function get($url, $</span><span class="attribute"><font color="#ff0000">userAgent</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">httpHeaders</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">username</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>, $</span><span class="attribute"><font color="#ff0000">password</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">''</font></span><span>)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ret</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">Curl_Class</font></span><span>::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);  </span>
</li>
<li class="">
<span>if (</span><span class="attribute"><font color="#ff0000">false</font></span><span> === $ret)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>return false;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>if (is_array($ret))  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>return false;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>return $ret;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>function create()  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">ch</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt"><span>if (!function_exists('curl_init'))  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>return false;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ch</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_init</font></span><span>();  </span>
</li>
<li class=""><span>if (!is_resource($ch))  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>return false;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>return $ch;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Nach dem Login kopieren

PHP递归数组代码:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span>    </span></span></li>
<li class=""><span>ignore_user_abort();//连线中断后(例如关闭浏览器)仍然继续执行以下的脚本直到处理完毕。  </span></li>
<li class="alt"><span>set_time_limit(0);  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">get_data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">file_get_contents</font></span><span>("php://input");  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute-value"><font color="#0000ff">explode</font></span><span class="attribute"><font color="#ff0000">explodedata</font></span><span> = explode("&", $get_data);  </span>
</li>
<li class="">
<span>foreach ($explodedata as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value)//还原数组  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>list($realkey, $realvalue) = explode("=", $value);  </span></li>
<li class="alt"><span>$data[urldecode($realkey)] = urldecode($realvalue);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>//现在$data数组已经和a.php中的一样了,接下来,就可以根据自己的需要对$data数组进行操作了。  </span></li>
<li class=""><span>//......  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Nach dem Login kopieren

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446465.htmlTechArticle我们大家都知道PHP是一种HTML内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage