Home > Backend Development > PHP Tutorial > About PHP recursive array code analysis_PHP tutorial

About PHP recursive array code analysis_PHP tutorial

WBOY
Release: 2016-07-15 13:28:21
Original
1140 people have browsed it

We all know that PHP is an HTML embedded language. PHP is quite similar to Microsoft's ASP. They are both scripting languages ​​that are executed on the server side and embedded in HTML documents. The language The style is similar to C language and is now widely used by many website programmers. This article introduces PHP recursive arrays in detail. The PHP program needs to write the received data to both the "formal database for online operation" and the "test database for development and debugging".

The test database may often face problems such as adjusting the table structure, fields, and configuration information. It is very unstable and has a high probability of errors. If Using the a.php program to write the "formal database" and "test database" at the same time will inevitably affect the formal services running online. So, I thought of using the PHP curl extension library to post a copy of the generated $data array to the PHP program, and then the PHP program continued to execute the code to write the "formal database". The PHP program passes the $data array to the PHP program and that's it. As for how PHP handles it, it has nothing to do with PHP. Even if the PHP program fails to write the "test database", it will not have an impact on the PHP program.

PHP recursive array source code:

<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 class=""><SPAN>$data["username"]="张宴";  </SPAN><LI class=alt><SPAN>$data["password"]="不知道";  </SPAN><LI class=""><SPAN>$data["ip"]="192.168.0.18";  </SPAN><LI class=alt><SPAN>//reGISter_shutdown_function("post_data", $data);  </SPAN><LI class=""><SPAN>//function post_data($data)  </SPAN><LI class=alt><SPAN>//{  </SPAN><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></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>post</FONT></SPAN><SPAN> = @$curl-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></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>
Copy after login

PHP recursive array code:

<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 class=""><SPAN>ignore_user_abort();//连线中断后(例如关闭浏览器)仍然继续执行以下的脚本直到处理完毕。  </SPAN><LI class=alt><SPAN>set_time_limit(0);  </SPAN><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></SPAN><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></SPAN><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>></span></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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446465.htmlTechArticleWe all know that PHP is an HTML embedded language. PHP is quite similar to Microsoft's ASP. Similar, they are both scripting languages ​​embedded in HTML documents that are executed on the server side. The style of the language...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template