search
HomeBackend DevelopmentPHP TutorialAbout PHP recursive array code analysis_PHP tutorial

About PHP recursive array code analysis_PHP tutorial

Jul 15, 2016 pm 01:28 PM
ahtmlphpandcodeaboutanalyzeMicrosoftusarrayyesofKnowlanguagerecursion

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

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

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...
Statement
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.